0

我正在做一个 android 计划应用程序,我使用 AlarmManager,但我的闹钟不起作用,这是我的代码:

我的活动:

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR, 1);
    calendar.set(Calendar.SECOND, 44);
    calendar.set(Calendar.AM_PM, Calendar.PM);

    pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, MyBroadCastRcv.class), 0);
    getApplicationContext();
    AlarmManager alarmMngr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmMngr.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

MyBroadCastRcv 类:

    public void onReceive(Context context, Intent intent)
{
    Intent serviceLauncher = new Intent(context, TimerService.class);
    context.startService(serviceLauncher);

}

我的服务:

public int onStartCommand(Intent intent, int flags, int startId)
{
    super.onCreate();
MediaPlayer player = MediaPlayer.create(this, Uri.parse("file:///mnt/sdcard/sherif/001.mp3"));

    return super.onStartCommand(intent, flags, startId);
}

public void onStart(Intent intent, int startId)
{
    System.out.println("In onStart Service");
    super.onStart(intent, startId);
    player.start();
}

我错过了什么吗?

4

1 回答 1

1

好吧,你肯定不想打电话super.onCreate()onStartCommand()删除它。

此外,您onStart()将永远不会被调用。此方法已弃用。您需要将所有代码放入onStartCommand().

于 2013-11-10T11:43:46.100 回答