簡體   English   中英

AlarmManager似乎不起作用

[英]AlarmManager doesn't seem to be working

我正在嘗試安排要使用AlarmManager調用的方法,但是它似乎沒有用。 我看過其他示例,但這些示例對我沒有用。 因此,我認為這是我的代碼中的某些內容。 這是AlarmManager代碼:

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(SplashScreenActivity.this, KinectReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(SplashScreenActivity.this, 0, intent, 0);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 25);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
        calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

我的廣播接收器:

public class KinectReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_stat_notify)
                .setContentTitle("Kinect")
                .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                .setWhen(System.currentTimeMillis())
                .setContentText("Your Kinects and Likes have been refilled. Now get to swiping")
                .setAutoCancel(true)
                .setSound(uri)
                .setStyle(new NotificationCompat.BigTextStyle()
                        .bigText("Your Kinects and Likes have been refilled. Now get to swiping"))
                .setVibrate(new long[] { 100, 500, 100, 500, 100 });

        Intent targetIntent = new Intent(context, MainActivity.class);
        targetIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        targetIntent.putExtra("action", "main");
        targetIntent.putExtra("id", "");
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        builder.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());
    }
}

吐司或通知均未顯示。

如果有AlarmManager代碼是在我的啟動器活動中首先運行的代碼。 謝謝

請像這樣在清單文件中注冊您的接收者

在應用標簽中

<receiver
   android:name=".KinectReceiver">

第二個問題是如何取消警報

這是你的答案

Intent myIntent = new Intent(MainActivity.this, AlarmActivity.class);
   pendingIntent = PendingIntent.getActivity(CellManageAddShowActivity.this,
       id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   pendingIntent.cancel();
   alarmManager.cancel(pendingIntent);

您將需要的主要內容是:

1)。創建具有相同ID和適當意圖FLAG的待定意圖。 2)。取消該未決意圖。 3)。使用警報管理器取消警報。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM