Android 台灣中文網
標題:
如何製作出使用KEYCODE_BACK關閉stopservice
[打印本頁]
作者:
mechwarr
時間:
2013-12-14 19:35
標題:
如何製作出使用KEYCODE_BACK關閉stopservice
小弟不才,java沒有學很好
但之前有寫過成功過,但現在卻忘記並且以前java檔不知道丟哪去了...
public class MainActivity extends Activity {
private SoundPool soundPool;
private int playsound;
private ImageButton Button01 ,Button02;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setClass(this, MusicService.class);
//start background music
startService(intent);
Button01 = (ImageButton)findViewById(R.id.imageButton1);
Button02 = (ImageButton)findViewById(R.id.imageButton2);
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 5);
playsound = soundPool.load(MainActivity.this, R.raw.touch, 1);
//-------------------------------------------------------------------------btn event
Button01.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
soundPool.play(playsound,1,1,0,0,1);
Intent intent = new Intent( MainActivity.this ,viva_menu.class);
startActivity(intent);
}
});
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------btn event
Button02.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
soundPool.play(playsound,1.0F, 1.0F, 0, 0, 1.0F);
// Intent intent = new Intent( MainActivity.this ,tutorial.class);
// startActivity(intent);
}
});
//-------------------------------------------------------------------------
// AnimationSet animationSet = new AnimationSet(true);
// TranslateAnimation translateAniamation= new TranslateAnimation(
// Animation.RELATIVE_TO_SELF,0f,
// Animation.RELATIVE_TO_SELF,-0.3f,
// Animation.RELATIVE_TO_SELF,0f,
// Animation.RELATIVE_TO_SELF,-0.2f
// );
// AlphaAnimation alpaAnimation = new AlphaAnimation(0,1);
//
// animationSet.setDuration(750);
// animationSet.setFillAfter(true);
// animationSet.addAnimation(translateAniamation);
// animationSet.addAnimation(alpaAnimation);
// Button01.startAnimation(animationSet);
// Button02.startAnimation(animationSet);
//-------------------------------------------------------------------------animation
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode==KeyEvent.KEYCODE_BACK)
{
soundPool.play(playsound,1.0F, 1.0F, 0, 0, 1.0F);
new AlertDialog .Builder(this) .setMessage("確定離開?") .setPositiveButton("確定", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
finish();
System.exit(0);
}
}).setNegativeButton("取消s", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
}
}).show();
}
return true;
}
}
複製代碼
這樣好像並不會關閉stopservice,因為還是有發現後端有在工作。
作者:
mechwarr
時間:
2013-12-14 19:41
阿,不好意思
已解決,只要輸入:
stopService(new Intent(MainActivity.this, Service.class));
既可
但不懂為什麼還要再 new Intent...
歡迎光臨 Android 台灣中文網 (https://apk.tw/)
Powered by Discuz! X3.1