【发布时间】:2015-07-07 15:37:58
【问题描述】:
我正在开发一个在后台触发通知的应用程序。即使设备处于睡眠状态。我使用了警报管理器和服务类。这是代码- 这些在活动类中。
public void start(){
scheduleNotification(getNotification(w.getMEAN()),time);
}
private void scheduleNotification(Notification notification, int delay) {
AlarmManager alarmManager = (AlarmManager)getSystemService(getBaseContext().ALARM_SERVICE);
int d=delay*1000;
Intent notificationIntent = new Intent(getApplicationContext(), NotificationPublisher.class);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
long futureInMillis = SystemClock.elapsedRealtime() + d;
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}
private Notification getNotification(String content) {
Intent intent=new Intent(getApplicationContext(),MeaningActivity.class);
id++;}
PendingIntent pi=PendingIntent.getActivity(this, 10, intent,PendingIntent.FLAG_CANCEL_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
if(id<15){
builder.setContentIntent(pi);
builder.setAutoCancel(true);
notiId++;
}
return builder.build();
}
通过按钮点击意图调用服务
case R.id.button1:
{
String t=ed.getText().toString();
int time = Integer.parseInt(t);
savePreferences("switch", s.isChecked());
if (s.isChecked())
{
savePreferences("time", ed.getText().toString());
Intent intent = new Intent(this,NotifyService.class);
//intent.putExtra("time",time);
startService(intent);
}
else
{
stopService(new Intent(this,NotifyService.class));
}
这是通知 publisher.java-
public class NotificationPublisher extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
List<Word> quesList;
int id=0,count =0;
Word w;
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND|Notification.DEFAULT_VIBRATE;
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
}
服务类是-
@Override
public IBinder onBind(Intent arg0) {
//time = arg0.getIntExtra("time",0);
return null;
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this,"Service created", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//start();
// Restart the service if got killed
sa.start();
Toast.makeText(this,"Service started", Toast.LENGTH_LONG).show();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"Service destroyed", Toast.LENGTH_LONG).show();
}
上面的代码显示了一个错误,说明服务不能被意图调用! 请帮助我了解如何在指定时间在后台触发通知.. 提前谢谢..
【问题讨论】:
标签: android android-intent notifications