Android的报警,服务不被称为

问题描述:

在我的活动:Android的报警,服务不被称为

Intent myIntent = new Intent(this, MyAlarmService.class); 
pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); 

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 

Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.add(Calendar.SECOND, 10); 
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

Toast.makeText(this, "Start Alarm", Toast.LENGTH_LONG).show(); 

的类被称为:

public class MyAlarmService extends Service { 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show(); 
     return null; 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     // TODO Auto-generated method stub 
     super.onStart(intent, startId); 
     Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show(); 
    } 
} 

有了这个代码,MyAlarmService不会被调用。为什么? 此代码已从样本中删除,因此测试的功能正常。我会忘记什么吗?

我认为,您的服务已正常启动。 Toast服务存在问题。

尝试将Toast.makeText()更改为Log.d()并查看记录是否出现在您的Logcat中。

烤面包在服务中的问题已被解释为here

这似乎是正确的。我也在我的代码中试过这个例子,它工作正常。

我认为问题出现在清单中。你需要这样的申报服务中体现你的项目:

< service android:name=".MyAlarmService" /> 
+0

是没有问题的,我的表现有行...现在我找到其他的方式来解决这个问题,谢谢;) – marceloamx