android - AlarmManager.RTC_WAKEUP 没有唤醒设备?

标签 android service broadcastreceiver alarmmanager

我一直在尝试使用 AlarmManager.RTC_WAKEUP 来唤醒我的设备以播放一些声音。 这是闹钟安排:

Intent intent = new Intent(Main.this, OneShotAlarm.class); 
PendingIntent sender =  PendingIntent.getBroadcast(Main.this, 0, intent, 0); 

// I want the alarm to go off 60 seconds from now. 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.add(Calendar.SECOND, 60); 

// Schedule the alarm! 
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 
am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(), sender); 

OneShotAlarm BroadCastReceiver:

public class OneShotAlarm extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
        Toast.makeText(context, "OneShotAlarm Broadcast invoked", 1000).show(); 

        intent = new Intent(context, ServiceClassToRun.class); 

        context.startService(intent); 
    } 
}

服务:

public class ServiceClassToRun extends Service { 
    protected static final int Rnd = 0; 

     public void onStart(Intent intent, int startId) { 
     //do some business here 
} 

最后,我的 list :

<receiver android:name=".OneShotAlarm" android:process=":remote" /> 
<service android:name=".ServiceClassToRun "/> 

当设备处于唤醒状态时,我的服务工作正常。 非常感谢任何想法。有没有其他人经历过这个 出色地?提前致谢。

最佳答案

PendingIntent sender =  PendingIntent.getBroadcast(Main.this, 0, intent, 0);

改为

PendingIntent sender =  PendingIntent.getBroadcast(Main.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

或者,如果你错过了警报的权限,那么添加这些权限

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>

让我知道,如果它适合你

关于android - AlarmManager.RTC_WAKEUP 没有唤醒设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136996/

相关文章:

C# Windows 服务 XML

android - 检查当前是否显示 XML 布局?

android - 将应用程序中的电话号码链接到拨号器 (Android)

android - 以编程方式膨胀 TextView 和 LinearLayout

oop - 帮助!邪恶的服务正在杀死我的对象

android - 我们可以用什么来代替 SystemClock.elapsedRealtime() 以获得可靠的时间?

android - 如何使用 XMPP 在 android 中执行群聊

java - 为什么 java Android 中的 super.onDestroy() 在析构函数中居于首位?

Android 广播接收器与 aidl

android - 如何在android中修改来电和去电屏幕 View