我创建了一个警报服务,以便在预定义的设定时间内生成消息。我的课程如下。我在我的主类中调用了setAlarmNotification。我还在我的Manifest.xml中为服务添加了2行。但是警报仍然没有工作,消息也没有被解雇。请指教。
public class AlarmTask implements Runnable {
private final Calendar date;
private final AlarmManager am;
private final Context context;
private final String taskToNotify;
public AlarmTask(Context context, Calendar date, String task) {
this.context = context;
this.am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
this.date = date;
this.taskToNotify = task;
}
@Override
public void run() {
Intent intent = new Intent(context, NotifyService.class);
intent.putExtra(NotifyService.INTENT_NOTIFY, true);
intent.putExtra("task", taskToNotify);
PendingIntent pendingIntent = PendingIntent.getService(context, 0,
intent, 0);
am.set(AlarmManager.RTC, date.getTimeInMillis(), pendingIntent);
}
}
以下是我的NotifySerice.java类
public class NotifyService extends Service {
public class ServiceBinder extends Binder {
NotifyService getService() {
return NotifyService.this;
}
}
private static final int NOTIFICATION = 123;
public static final String INTENT_NOTIFY = "com.example.tasker.INTENT_NOTIFY";
private NotificationManager mNM;
private String task;
public void onCreate() {
Log.i("NotifyService", "onCreate()");
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getBooleanExtra(INTENT_NOTIFY, false))
task = intent.getStringExtra("task");
showNotification();
return START_NOT_STICKY;
}
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new ServiceBinder();
private void showNotification() {
CharSequence title = "Reminder!";
int icon = R.drawable.ic_dialog_alert;
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_dialog_alert);
CharSequence text = task;
long time = System.currentTimeMillis();
PendingIntent contentIntent = PendingIntent
.getActivity(this, new Random().nextInt(), new Intent(this,
ViewTask.class), 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
this)
.setWhen(time)
.setContentText(text)
.setContentTitle(title)
.setSmallIcon(icon)
.setAutoCancel(true)
.setTicker(title)
.setLargeIcon(largeIcon)
.setDefaults(
Notification.DEFAULT_LIGHTS
| Notification.DEFAULT_VIBRATE
| Notification.DEFAULT_SOUND)
.setContentIntent(contentIntent);
Notification notification=notificationBuilder.build();
mNM.notify(new Random().nextInt(), notification);
stopSelf();
}
}
这是我的scheduleClient.java类:
public class ScheduleClient {
private ScheduleService mBoundService;
private Context mContext;
private boolean mIsBound;
public ScheduleClient(Context context) {
mContext = context;
}
public void doBindService() {
// Establish a connection with our service
mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((ScheduleService.ServiceBinder) service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
public void setAlarmForNotification(Calendar c, String task){
mBoundService.setAlarm(c, task);
}
public void doUnbindService() {
if (mIsBound) {
// Detach our existing connection.
mContext.unbindService(mConnection);
mIsBound = false;
}
}
}
最后我的ScheduleService.java类:
public class ScheduleService extends Service {
public class ServiceBinder extends Binder {
ScheduleService getService() {
return ScheduleService.this;
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new ServiceBinder();
public void setAlarm(Calendar c, String task) {
new AlarmTask(this, c,task).run();
}
}