2016-07-06 43 views
0

我有在每個Chrono.change(目前測試目的)發送批註的應用程序。它的工作很好,當我在MainActivity的功能開發。但是它不適用於任何其他活動或者當按下HOME按鈕時。如果按下POWER按鈕,應用程序將在後臺運行並且工作正常(如果在MainActivity上按POWER按鈕)。當(andorid)主頁按鈕被按下時(背景)如何顯示註釋

任何想法如何解決這個兩個問題:

1)通知還發送,如果在另一個活動

2)更關鍵的IM - 通知是即使按HOME鍵發送和林不目前在應用中。

eventL istener:

stopWatch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() { 
     @Override 
     public void onChronometerTick(Chronometer chronometer) { 
      turnedOnOff = prefs.getBoolean("notification",false); 
      if (turnedOnOff) 
       throwNotification(); 
     } 
    }); 

Nottifications:

public void throwNotification() 
{ 
    // Prepare intent which is triggered if the 
    // notification is selected 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.setAction(Intent.ACTION_MAIN); 
    intent.addCategory(Intent.CATEGORY_LAUNCHER); 


    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 
      intent, 0); 

    // Build notification 
    // Actions are just fake 
    Notification noti = new Notification.Builder(this) 
      .setContentTitle("Finish!") 
      .setContentText("Its done").setSmallIcon(R.mipmap.notif) 
      .setContentIntent(pendingIntent) 
        .build(); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    // hide the notification after its selected 
    noti.flags |= Notification.FLAG_AUTO_CANCEL; 
    noti.defaults |= Notification.DEFAULT_SOUND; 
    noti.defaults |= Notification.DEFAULT_VIBRATE; 

    notificationManager.notify(0, noti); 
    } 

感謝暢想!

回答

0

通過@Raghunandan回答

這是不可能的。

除非您將應用程序設置爲主屏幕,否則無法攔截Android上的主頁按鈕。這是出於安全原因,因此惡意應用無法通過覆蓋可退出的所有按鈕來接管設備。

如果要處理HOME按鈕,請實施主屏幕。

但我建議你重新考慮你的設計。將註銷選項留給用戶。

http://developer.android.com/training/design-navigation/index.html

看看Gmail應用。當您按Home鍵時,它不會註銷。

@Override 
protected void onUserLeaveHint() 
{ 
     super.onUserLeaveHint(); 
     Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show(); 
     Log.i("Home button Pressed!", "Yes"); 
     finish(); 
} 

您可以使用onUserLeaveHint

保護無效onUserLeaveHint()

在API級別3

調用作爲活動週期的一部分,當活動即將進入後臺爲用戶選擇的結果。例如,當用戶按下主頁鍵時,onUserLeaveHint()將被調用,但當來電打電話導致通話中的Activity被自動帶到前臺時,onUserLeaveHint()將不會被中斷的活動調用。在被調用的情況下,該方法在活動的onPause()回調之前被調用。

此回調和onUserInteraction()旨在幫助活動智能地管理狀態欄通知;特別是幫助活動確定取消通知的適當時間。

+0

感謝您的回覆,螺母我不需要重新編程主頁按鈕。我只是需要我的應用程序能夠發送通知,即使主頁按鈕被按下,我目前不在應用程序 –

+0

@jan我的答案包括如何做到這一點。 – GoogleMac

相關問題