The*_*ude 4 service android alarmmanager android-intent android-pendingintent
我遇到问题让我pendingIntent着火了.我已经使用logcat等进行了一些故障排除,最后我几乎肯定我的问题实际上是在我的pendingIntent方法中.我设置的时间是正确的,并且该方法被调用,但在预定的时间没有任何事情发生.这是我用来创建的方法pendingIntent
public void scheduleAlarm(){
Log.d("Alarm scheduler","Alarm is being scheduled");
Intent changeVol = new Intent();
changeVol.setClass(this, VolumeService.class);
PendingIntent sender = PendingIntent.getService(this, 0, changeVol, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, time, sender);
//Toast.makeText(this, "Volume Adjusted!", Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)
这是服务类:
public class VolumeService extends Service{
@Override
public void onCreate() {
super.onCreate();
Log.d("Service", "Service has been called.");
Toast.makeText(getApplicationContext(), "Service Called!", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
scheduleAlarm()班级中的日志按照我的计划运行,但后来没有任何反应,所以我认为它是我的pendingIntent.提前致谢!
弄清楚了!问题出在Service类中,我也改变了一些其他的东西.但是,我认为主要问题是在我的服务类中,onCreate我试图运行我的代码.但这需要在该onStartCommand方法中完成
public class VolumeService extends Service{
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();
return START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
并且在启动服务的课程中进行了一些更改,如下所示:
public void scheduleAlarm(){
Log.d("Alarm scheduler","Alarm is being scheduled");
Intent intent = new Intent(AlarmSettings.this, VolumeService.class);
PendingIntent pintent = PendingIntent.getService(AlarmSettings.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.set(AlarmManager.RTC_WAKEUP, time, pintent);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13552 次 |
| 最近记录: |