应用程序正在使用Alarm Manager打开通知创建

时间:2015-02-06 13:17:34

标签: android alarmmanager android-notifications

以下是我要做的事情:

我试图在用户可以在设置屏幕中选择的特定时间每天触发通知。我目前面临的问题是,只要警报触发并显示通知,应用程序就会打开和关闭。

请在下面找到我使用过的代码: 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();
    }
}

1 个答案:

答案 0 :(得分:0)

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

您向系统提供了打开活动的说明。

您应该使用创建BroadcastReceiver,在AndroidManifest.xml上注册并为其创建一个意图new Intent(activity, NotificationBroadcast.class);

然后在onReceive回调内部生成并显示通知。