2016-12-27 24 views
1

我跟着RadioPlayerService作爲例子來創建機器人通知不與應用程序android關閉?

無線電流我已經添加下面的代碼在RadioActivity.java

@Override 
    public void onBackPressed() { 

     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
     alertDialogBuilder.setTitle("Exit ! Radio App."); 
     alertDialogBuilder 
       .setMessage("Click Yes to Exit!") 
       .setCancelable(false) 
       .setPositiveButton("Yes", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           moveTaskToBack(true); 
           android.os.Process.killProcess(android.os.Process.myPid()); 
           System.exit(0); 

          } 
         }) 

       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         dialog.cancel(); 
        } 
       }); 

     AlertDialog alertDialog = alertDialogBuilder.create(); 
     alertDialog.show(); 

    } 

這裏退出我的應用程序正在退出但通知是工作this是我的通知類

任何人都可以告訴我如何在出口處殺死整個應用程序

或合併這兩個通知服務和主要活動,這樣我可以退出該應用程序完全

This是完整的參考代碼

任何一個可以建議我對這種

更新1

我在主要活動中提供退出選項而不是其他服務 我的主要活動是RadioActivity.java而我的服務是RadioPlayerService.java

所以我在這裏給退出主要活動,所以我需要的是我的意思,而不是退出主要活動它應該退出整個應用程序,包括服務。

不要反對票不加評論,沒有看到全sourcode不評論..

再次感謝

+0

你的意思是,當你去從後面的活動要清除所有的通知,停止播放? –

+0

是換句話說,它退出或強制關閉通知時,應用程序退出或關閉 –

+0

'android.os.Process.killProcess(android.os.Process.myPid()); System.exit(0);'是不好的方式來關閉活動 – Gattsu

回答

2

您已經使用System.exit(0);,我想建議你不要使用它。使用它不是一種好的編程風格。有一個叫finish();

  • 你並不需要調用System.exit(0)終止的Activity方法,只是Activity.finish()

關閉應用程序

Intent homeIntent = new Intent(Intent.ACTION_MAIN); 
homeIntent.addCategory(Intent.CATEGORY_HOME); 
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(homeIntent); 

你可以讓你活動作爲單個實例,以便您從任何地方撥打設備將返回相同的活動疊加。

在的Manifest.xml

「...機器人:launchMode =」 singleInstance「>

爲了避免Intent調用同一Activity的多個實例從Intent除去FLAG_ACTIVITY_NEW_TASK標誌,並添加FLAG_ACTIVITY_CLEAR_TOP一個

從文件:

FLAG_ACTIVITY_CLEAR_TOP
如果設置,以及正在啓動的活動是 已經在當前任務運行,則代替開展該活動的一個新 例如,所有在它 上面的其他活動的將被關閉,該意圖將交付到(現在上面) 舊活動作爲一個新的意圖。

FLAG_ACTIVITY_NEW_TASK
如果設置,此活動將成爲此歷史堆棧上新任務的開始。


清除所有通知

NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
nMgr.cancelAll(); 

UPDATE1

@Override 
public void onBackPressed() { 

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    alertDialogBuilder.setTitle("Exit ! Radio App."); 
    alertDialogBuilder 
      .setMessage("Click Yes to Exit!") 
      .setCancelable(false) 
      .setPositiveButton("Yes", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

          mRadioManager .disconnect();// after adding this its also closed radio player 
          NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
          nMgr.cancelAll(); 
          Intent homeIntent = new Intent(Intent.ACTION_MAIN); 
          homeIntent.addCategory(Intent.CATEGORY_HOME); 
          homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
          startActivity(homeIntent); 

         } 
        }) 

      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 

        dialog.cancel(); 
       } 
      }); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 

} 
+1

其實我給退出選項主要活動本身radioactivty.java你能不能更新你的回答 –

+1

@ Don'tBenegative請檢查我已更新的完整解決方案這可能有所幫助。如果你仍然有prb然後評論在這裏:-) #soReadyToHelp *請不要downvote請* – Gattsu

+1

謝謝@Manish Yadav先生......你的代碼工作很好,但廣播播放器沒有關閉,但添加後它退出每一件事mRadioManager .disconnect(); –

0

所以基本上你可以做的是,當您單擊Yes你的對話框,您應該通過調用finish()來終止你的活動,並且你必須停止你的服務,或者如果它被你的Activity綁定,你可以避免調用stopService,並且當你的活動被調用Finish()時它會自動解除綁定,代碼如下

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
     alertDialogBuilder.setTitle("Exit ! Radio App."); 
     alertDialogBuilder 
       .setMessage("Click Yes to Exit!") 
       .setCancelable(false) 
       .setPositiveButton("Yes", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           moveTaskToBack(true); 
           //this will unbind the radioService 
           mRadioManager.disconnect(); 
           finish(); 

          } 
         }) 

       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 

         dialog.cancel(); 
        } 
       }); 

     AlertDialog alertDialog = alertDialogBuilder.create(); 
     alertDialog.show(); 

在您的RadioPlayerService,覆蓋的onDestroy方法,並呼籲公衆方法stopFromNotification()方法也

+0

這裏什麼是your_service_action意圖我的通知是[this](https://github.com/iammert/RadioPlayerService/blob/master/library/src/main/java/co/mobiwise/library/radio/RadioPlayerService.java)你能否更新你的答案 –

+0

@ Don'tBenegative你可以告訴我你的RadioActivity,你是如何啓動你的RadioService或者你可以粘貼你的RadioActivity的鏈接。我會更新相應的:) –

+0

請參閱無線電服務是在圖書館,但在我的代碼的整個代碼我結合起來都看到完整的源代碼和upvote不downvote plese沒有引用 –

相關問題