简体   繁体   English

使用AlarmManager运行通知

[英]Running a notification using AlarmManager

I'm using the alarm manager to request data from a server periodically. 我正在使用警报管理器定期从服务器请求数据。 I want to display the response in a notification. 我想在通知中显示响应。 However, the app crashes whenever I try to display the notification. 但是,每当我尝试显示通知时,应用程序就会崩溃。 This is the code I've written: 这是我编写的代码:

Main 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();
    }
}

And the Alarm 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
    }

I'm a complete Android newbie so any help would be appreciated. 我是一个完整的Android新手,所以我们将不胜感激。

It looks like you are attempting to set an inexact alarm that repeats every 10 seconds. 您似乎正在尝试设置一个不精确的警报,该警报每10秒重复一次。

Try changing your Alarm class to the following, being sure to put in your package name, and your packageName.ActivityNameToLaunch where I have specified: 尝试将您的Alarm类更改为以下内容,确保输入您的包名称以及我指定的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);
     }
}

This is how I have a working alarm set up in one of my apps. 这就是我在其中一个应用程序中设置工作警报的方式。 Please note this code will launch an activity that you specify when the alarm goes off. 请注意,此代码将在闹钟响起时启动您指定的活动。 In that activity you can have your Toast notification, or whatever you want. 在该活动中,您可以发送Toast通知,也可以发送任何通知。

Also I think you need an AlarmService class to handle the intent: 我也认为您需要一个AlarmService类来处理意图:

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());
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM