繁体   English   中英

使用AlarmManager运行通知

[英]Running a notification using AlarmManager

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

主要活动

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

和警报类

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秒重复一次。

尝试将您的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);
     }
}

这就是我在其中一个应用程序中设置工作警报的方式。 请注意,此代码将在闹钟响起时启动您指定的活动。 在该活动中,您可以发送Toast通知,也可以发送任何通知。

我也认为您需要一个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