![](/img/trans.png)
[英]Running an IntentService in BroadcastReceiver using 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.