2015-02-06 176 views
0

這裏就通知創造開放是什麼,我試圖做的:應用程序使用報警管理

我想通知每天在特定的時間,用戶可以在選擇設置屏幕觸發。我目前面臨的問題是,只要報警觸發並顯示通知,應用程序就會打開並關閉。

請找到下面的代碼,我已經使用: MainActivity.java

@Override 
protected void onRestart() { 
    Util.handleNotificationPreferences(this); 
    super.onResume(); 
} 

Util.java

public static void handleNotificationPreferences(Activity activity) { 
    SharedPreferences notificationPreferences = activity.getSharedPreferences("com.xyz.abc_prefs, Context.MODE_PRIVATE); 
    boolean notificationEnabled = notificationPreferences.getBoolean("currencyNotificationOnOff", false); 

    if(notificationEnabled) { 
     String time = notificationPreferences.getString("scheduleNotificationTime", ""); 
     String hour = time.split(":")[0]; 
     String minutes = time.split(":")[1]; 
     Calendar calendar = Calendar.getInstance(); 

     calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour)); 
     calendar.set(Calendar.MINUTE, Integer.parseInt(minutes)); 
     calendar.set(Calendar.SECOND, 0); 

     Calendar now = Calendar.getInstance(); 

     if(calendar.getTimeInMillis() < now.getTimeInMillis()) { 
      calendar.setTimeInMillis(calendar.getTimeInMillis() + 86400000L); 
     } 

     AlarmManager alarmManager = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE); 

     //---PendingIntent to launch activity when the alarm triggers--- 
     Intent i = new Intent(activity, NotificationActivity.class); 

     //---assign an ID of 1--- 
     i.putExtra("notificationId", 1); 

     PendingIntent displayIntent = PendingIntent.getActivity(
       activity.getBaseContext(), 0, i, 0); 

     //---sets the alarm to trigger--- 
     alarmManager.set(AlarmManager.RTC_WAKEUP, 
       calendar.getTimeInMillis(), displayIntent); 
    } 
} 

NotificationActivity.Java

public class NotificationActivity extends FragmentActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     int notificationId = getIntent().getExtras().getInt("notificationId"); 

     Intent resultIntent = new Intent(this, MainActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , resultIntent,0); 


     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentTitle(getResources().getString(R.string.notification_title)) 
      .setContentText(getResources().getString(R.string.notification_text)) 
      .setContentIntent(pendingIntent) 
      .setAutoCancel(true) 
      .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE); 

     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ 
      mBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); 
     } 

     // Gets an instance of the NotificationManager service 
     NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     // Builds the notification and issues it. 
     mNotifyMgr.notify(notificationId, mBuilder.build()); 

     finish(); 
    } 
} 

回答

0

Intent i = new Intent(activity, NotificationActivity.class);

您正在向系統提供打開活動的說明。

您應該使用,而不是創建一個BroadcastReceiver,註冊它的AndroidManifest.xml並創建一個意圖將new Intent(activity, NotificationBroadcast.class);

那麼onReceive再打你裏面產生,並顯示該通知。

+0

感謝您的回覆..我會試一試 – Nagamohan 2015-02-09 12:41:33

+0

由於您的建議,此問題已得到解決。我將代碼移到了BroadcastReceiver類中,並在Util.java中爲PendingIntent調用了'getBroadcast'而不是'getActivity'。 – Nagamohan 2015-02-09 12:57:52