2016-01-24 96 views
0

我正在开发闹钟应用程序,在不同时刻多次提醒用户。一切工作都很完美,除了当我断开手机与电脑并关闭屏幕(例如按下电源按钮),警报不会关闭。但是当我按下按钮手动唤醒电话时,闹钟突然熄灭。Android AlarmManager RTC_WAKEUP不醒来CPU

这发生在Android 4.4,API 19(KITKAT)上。在Android 2.3手机上每次都会正确唤醒。

请帮帮我。如何使它适用于所有Android版本?

MainActivity.java:

Button b = (Button) findViewById(R.id.button); 
    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      setAlarm(0); 
      setAlarm(1); 
      setAlarm(4); 
      setAlarm(7); 
     }}); 

public void setAlarm(int index) { 
    long tim = new GregorianCalendar().getTimeInMillis() + 5 * 1000 * (index+1); 
    Intent intent = new Intent(this, MyReceiver.class); 
    intent.setData(Uri.parse(Integer.toString(index))); 
    PendingIntent pi = PendingIntent.getBroadcast(this, index, intent, PendingIntent.FLAG_ONE_SHOT); 
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE); 
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { 
     am.setExact(AlarmManager.RTC_WAKEUP, tim, pi); 
    } else { 
     am.set(AlarmManager.RTC_WAKEUP, tim, pi); 
    } 
} 

MyReceiver.java:

public void onReceive(Context context, Intent intent) { 
    Intent mIntent = new Intent(context, NotifActivity.class); 
    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(mIntent); 
} 

NotifActivity.java:

boolean done; 
public PowerManager.WakeLock mWakeLock; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    done = false; 
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); 
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | 
      PowerManager.ON_AFTER_RELEASE, "TEST_LOCK"); 
    mWakeLock.acquire(); 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_notif); 

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON); 

    final Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 
    vibe.vibrate(100); 

    done = true; 
} 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    if (done) { 
     try { 
      if (mWakeLock != null) 
       mWakeLock.release(); 
     } catch (Exception e) { 
      //e.printStackTrace(); 
     } 
    } 
} 

的AndroidManifest.xml:

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

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <receiver 
      android:name=".MyReceiver" 
      android:enabled="true" 
      android:exported="false" 
      android:process=":remote"/> 

     <activity android:name=".NotifActivity"></activity> 
    </application> 

回答

0

好的,问题解决了。多么愚蠢的错误......我在使用SONY XPERIA手机进行debbugging。它有一些奇怪的省电模式“STAMINA”打开,这可以防止任何不是来自SONY的应用程序唤醒手机。

所以代码是正确的。唯一可以添加的是onReceive方法中的另一个wakelock。