Android: - アラームマネージャーが機能していません
-
29-10-2019 - |
質問
私のコードアラームでは、Mangerが機能していません。アプリケーションのレストはうまく機能しています。コードを参照してください。
Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
myIntent.putExtra("class", "home");
PendingIntent pendingIntent = PendingIntent.getService(this, 0,myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);
そして私のAndroid AlarmServiceクラス: -
public class AndroidAlarmService extends BroadcastReceiver implements URLs{
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("BroadCast\n");
String name=intent.getStringExtra("class");
if(name.equals("home")){
Intent homeIn=new Intent(context,Home.class);
context.startActivity(homeIn);
}
}
}
マニフェストで私はこれをしました。
<receiver android:name=".AndroidAlarmService" android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"></action>
</intent-filter>
</receiver>
なぜ機能しないのですか?
解決
私は答えを得ました。私は次の変更を加えました:
Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent, 0);
私のAndroidAlarmServiceクラスで:
Intent homeIn=new Intent(context,Home.class);
homeIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIn);
他のヒント
一般的なものではなく、ブロードキャストレシーバーを別のパッケージに置いたことがわかったまで、同じ問題がありました。
簡単に変更しました:
<receiver android:name=".AndroidAlarmService" android:enabled="true" >
為に:
<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >
変更を試みましたか
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);
6000を何か他のものに変更してみましたか?他にすべて正しいものがあるようです。
編集:
read_phone_stateの許可がマニフェストにあることを確認してください。
所属していません StackOverflow