Alarmmanager Doesn`t Start Broadcastreceiver
I am trying to use BroadcastReceiver and AlarmManager to set a one-shot alarm. I have no idea why isn`t it working. What am I doing wrong? I have no exceptions, no logs about, no s
Solution 1:
try like this: it will fire alarm after 30 second.
publicclassTestAlarmManagerextendsActivity{
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm(getApplicationContext());
}
publicvoidsetAlarm(Context mContext) {
Intentintent=newIntent(this, AlarmReceiver.class);
PendingIntentpendingIntent= PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManageralarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (30 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + 30 + " seconds",Toast.LENGTH_LONG).show();
}
}
AlarmReceiver.java
publicclassAlarmReceiverextendsBroadcastReceiver {
@OverridepublicvoidonReceive(Context context, Intent arg1) {
Log.e("alarm","got into alarm receiver");
Toast.makeText(context, "Alarm Started..", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.think.androidteststackoverflow"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="18" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher_3"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.think.androidteststackoverflow.TestAlarmManager"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><receiverandroid:name=".AlarmReceiver"></receiver></application></manifest>
Post a Comment for "Alarmmanager Doesn`t Start Broadcastreceiver"