綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 如何製作出使用KEYCODE_BACK關閉stopservice

[複製連結] 查看: 1021|回覆: 1|好評: 0
跳轉到指定樓層
樓主
mechwarr | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2013-12-14 19:35

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
小弟不才,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,因為還是有發現後端有在工作。
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
 樓主| mechwarr | 收聽TA | 只看該作者
發表於 2013-12-14 19:41
阿,不好意思
已解決,只要輸入:
stopService(new Intent(MainActivity.this, Service.class));
既可

但不懂為什麼還要再 new Intent...
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則