这段代码有什么问题?我没有收到任何错误,代码无法正常工作?
MainActivity.java
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
Intent alertIntent = new Intent(MapsActivity.this, MyService.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime,
PendingIntent.getBroadcast(MapsActivity.this, 1, alertIntent,
PendingIntent.FLAG_UPDATE_CURRENT));
MyService.java
public class MyService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
createNotification(context, "Alarm", "ASDFG", "ASDF");
}
public void createNotification(Context context, String msg, String msgText, String msgAlert) {
PendingIntent notificIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MapsActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.searchicon)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}}
BTW我想在setOnMyLocationChangeListener
MainActivity.java
下的onCreate()
课程中启动警报和通知,有人可以帮助我在后台运行吗?
答案 0 :(得分:0)
我对这个问题的借口我实际上已将<receiver android:name=".MyService" ></receiver>
放在AndroidManifest.xml
的错误位置。它需要位于<application></application>
标记内。