2016-01-24 45 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。