Android 台灣中文網

標題: 如何製作出使用KEYCODE_BACK關閉stopservice [打印本頁]

作者: mechwarr    時間: 2013-12-14 19:35
標題: 如何製作出使用KEYCODE_BACK關閉stopservice
小弟不才,java沒有學很好
但之前有寫過成功過,但現在卻忘記並且以前java檔不知道丟哪去了...

  1. public class MainActivity extends Activity {
  2.        
  3.         private SoundPool soundPool;
  4.         private int playsound;
  5.         private ImageButton Button01 ,Button02;
  6.        
  7.         @Override
  8.         protected void onCreate(Bundle savedInstanceState) {
  9.                 super.onCreate(savedInstanceState);
  10.                 setContentView(R.layout.activity_main);
  11.                
  12.                
  13.                 Intent intent = new Intent();
  14.                 intent.setClass(this, MusicService.class);
  15.                 //start background music
  16.                 startService(intent);
  17.                
  18.                 Button01 = (ImageButton)findViewById(R.id.imageButton1);
  19.                 Button02 = (ImageButton)findViewById(R.id.imageButton2);
  20.                
  21.                 soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 5);
  22.                 playsound = soundPool.load(MainActivity.this, R.raw.touch, 1);
  23.                
  24. //-------------------------------------------------------------------------btn event
  25.                 Button01.setOnClickListener(new OnClickListener() {

  26.                   @Override
  27.                   public void onClick(View arg0) {
  28.                         soundPool.play(playsound,1,1,0,0,1);
  29.                     Intent intent = new Intent( MainActivity.this ,viva_menu.class);
  30.                     startActivity(intent);
  31.                     
  32.                   }

  33.                 });
  34. //-------------------------------------------------------------------------

  35. //-------------------------------------------------------------------------btn event
  36.                 Button02.setOnClickListener(new OnClickListener() {
  37.                  
  38.                   @Override
  39.                   public void onClick(View arg0) {
  40.                         soundPool.play(playsound,1.0F, 1.0F, 0, 0, 1.0F);
  41. //                    Intent intent = new Intent( MainActivity.this ,tutorial.class);
  42. //                    startActivity(intent);
  43.                   }
  44.                 });
  45. //-------------------------------------------------------------------------
  46.                
  47. //                AnimationSet animationSet = new AnimationSet(true);
  48. //                TranslateAnimation translateAniamation= new TranslateAnimation(
  49. //                                Animation.RELATIVE_TO_SELF,0f,
  50. //                                Animation.RELATIVE_TO_SELF,-0.3f,
  51. //                                Animation.RELATIVE_TO_SELF,0f,
  52. //                                Animation.RELATIVE_TO_SELF,-0.2f
  53. //                                );
  54. //                AlphaAnimation alpaAnimation = new AlphaAnimation(0,1);
  55. //               
  56. //                animationSet.setDuration(750);
  57. //                animationSet.setFillAfter(true);
  58. //                animationSet.addAnimation(translateAniamation);
  59. //                animationSet.addAnimation(alpaAnimation);
  60. //                Button01.startAnimation(animationSet);
  61. //                Button02.startAnimation(animationSet);
  62. //-------------------------------------------------------------------------animation               
  63.         }

  64.         @Override
  65.         public boolean onCreateOptionsMenu(Menu menu) {
  66.                 // Inflate the menu; this adds items to the action bar if it is present.
  67.                 getMenuInflater().inflate(R.menu.main, menu);
  68.                 return true;
  69.         }

  70.     public boolean onKeyDown(int keyCode, KeyEvent event)

  71.     {
  72.         if (keyCode==KeyEvent.KEYCODE_BACK)

  73.            {
  74.                 soundPool.play(playsound,1.0F, 1.0F, 0, 0, 1.0F);
  75.             new AlertDialog .Builder(this) .setMessage("確定離開?") .setPositiveButton("確定", new DialogInterface.OnClickListener()

  76.                  {  
  77.                 public void onClick(DialogInterface arg0, int arg1)

  78.                     {
  79.                     finish();
  80.                     System.exit(0);
  81.                     }

  82.                 }).setNegativeButton("取消s", new DialogInterface.OnClickListener()

  83.                 {  
  84.                public void onClick(DialogInterface arg0, int arg1)

  85.                    {  
  86.                     
  87.                    }
  88.                }).show();
  89.             }
  90.         return true;
  91.       }



  92. }
複製代碼


這樣好像並不會關閉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