Frage

In my code alarm manger is not working.Rest of my application is working well.Please see my code.

   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);

and my android AlarmService class:-

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);
     }

}
}

in manifest I have done this;

 <receiver android:name=".AndroidAlarmService" android:enabled="true" >
      <intent-filter>
          <action android:name="android.intent.action.PHONE_STATE"></action>
      </intent-filter>
 </receiver>

Why its not working??

War es hilfreich?

Lösung

I got the answer.I made following changes:

Intent myIntent = new Intent(getApplicationContext(), AndroidAlarmService.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent, 0);

In my AndroidAlarmService class:

Intent homeIn=new Intent(context,Home.class);
homeIn.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(homeIn);

Andere Tipps

I had the same problem until I found that I had put my Broadcast Receiver on a different package, not the general.

Simply changed:

<receiver android:name=".AndroidAlarmService" android:enabled="true" >

for:

<receiver android:name="com.MyCompany.MyPackage.AndroidAlarmService" android:enabled="true" >

Have you tried changing the

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),6000,pendingIntent);

Have you tried changing the 6000 to something else? It seems like you have everything else correct.

EDIT:

Make sure you have the Read_phone_state permission in your manifest.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top