我按照教程在一定时间内显示通知消息,但我不知道为什么它不起作用
这是方法
//notification
private void startAlarm() {
AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.MONTH, 12);
Calendar_Object.set(Calendar.YEAR, 2014);
Calendar_Object.set(Calendar.DAY_OF_MONTH, 31);
Calendar_Object.set(Calendar.HOUR_OF_DAY, 9);
Calendar_Object.set(Calendar.MINUTE, 06);
Calendar_Object.set(Calendar.SECOND, 0);
Intent intent = new Intent(ManagePassengers.this, AlarmReciver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ManagePassengers.this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), pendingIntent);
}
广播课
public class AlarmReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// When our Alaram time is triggered , this method will be excuted (onReceive)
// We're invoking a service in this method which shows Notification to the User
Intent myIntent = new Intent(context, NotificationService.class);
context.startService(myIntent);
}}
服务类
public class NotificationService extends Service {
private NotificationManager mManager;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// Getting Notification Service
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(), ManageRides.class);
Notification notification = new Notification(R.drawable.icon,
"See My App something for you", System.currentTimeMillis());
...
mManager.notify(0, notification);
}
....
}
清单编辑
<uses-permission android:name="android.permission.WAKE_LOCK" />
<activity android:name=".AlarmReciver" />
<activity android:name=".NotificationService" />
我等待我在日历对象中指定的时间但没有出现,我不知道我是否遗漏了任何东西
任何帮助将不胜感激
答案 0 :(得分:0)
您的服务和接收器组件注册错误。清单中的服务和接收器示例如下所示:
<service
android:name="MyService"
android:icon="@drawable/icon"
android:label="@string/service_name"
>
</service>
<receiver android:name="MyScheduleReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
查看以下教程: http://www.vogella.com/tutorials/AndroidServices/article.html http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html