运行 使用 AlarmManager 的通知

Running a notification using AlarmManager

我正在使用警报管理器定期从服务器请求数据。我想在通知中显示响应。但是,每当我尝试显示通知时,应用程序就会崩溃。这是我写的代码:

主要Activity

public class MainActivity extends AppCompatActivity {

    private WebView mWebview;
    private Alarm alarm;
    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        Intent alarmIntent = new Intent(MainActivity.this, Alarm.class);
        alarmIntent.putExtra(Alarm.NOTIFICATION_ID, 1);
        alarmIntent.putExtra(Alarm.NOTIFICATION, getNotification("HELLO"));
        pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);
        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10000, pendingIntent);
        Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
    }

    private Notification getNotification(String content) {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setContentTitle("Scheduled Notification");
        builder.setContentText(content);
        return builder.build();
    }
}

还有闹钟class

public class Alarm extends BroadcastReceiver 
    public static String NOTIFICATION_ID = "notification-id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "I'm running", Toast.LENGTH_SHORT).show();

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification); // !!!!! CRASHES HERE
    }

我是一个完整的 Android 新手,所以任何帮助将不胜感激。

您似乎在尝试设置每 10 秒重复一次的不准确闹钟。

尝试将您的警报 class 更改为以下内容,确保输入您的包裹名称,以及我指定的 packageName.ActivityNameToLaunch:

public class Alarm extends WakefulBroadcastReceiver {
     public void onReceive(final Context context, Intent intent) {
         Intent myIntent = new Intent();
         myIntent.setClassName("YourPackageName", "YourPackageName.ActivityToLaunchOnAlarmReceive");
         myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(myIntent);
     }
}

这就是我在我的一个应用程序中设置工作闹钟的方式。请注意,当闹钟响起时,此代码将启动您指定的 activity。在那个 activity 你可以有你的吐司通知,或者任何你想要的。

此外,我认为您需要一个 AlarmService class 来处理意图:

public class AlarmService extends IntentService {
    private NotificationManager alarmNotificationManager;

    public AlarmService() {
        super("AlarmService");
    }

    @Override
    public void onHandleIntent(Intent intent) {

            sendNotification("Wake up sleepyhead");

    }

    private void sendNotification(String msg) {
        alarmNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);

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

        NotificationCompat.Builder alarmNotificationBuilder = new NotificationCompat.Builder(
                this).setContentTitle("Reminder").setSmallIcon(R.mipmap.ic_launcher)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                .setContentText(msg);  

        alarmNotificationBuilder.setContentIntent(contentIntent);
        alarmNotificationManager.notify(1, alarmNotificationBuilder.build());
    } 
}