2016-06-11 20 views
0

Ich möchte jede X Minute eine Aufgabe ausführen. Ich benutze AlarmManager und es funktioniert gut, wenn App läuft oder im Hintergrund. Es funktioniert jedoch nicht mehr, nachdem ich die App aus der Liste der aktiven Apps entfernt habe.AlarmManager funktioniert nicht, wenn App geschlossen wird

Alarmeinstellung:

Intent intent = new Intent(context , SoundNotificationReceiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0 , intent, 0); 
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
Calendar c = Calendar.getInstance(); 
c.add(Calendar.MINUTE , 15); 

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() , pendingIntent); 
} 
else { 
    alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() , pendingIntent); 
} 

System.out.println("Alarm is set : " + c.getTime()); 

manifest.xml:

<receiver android:name="info_tech.com.fazeker.SoundNotificationReceiver" 
    android:exported="true" 
    android:enabled="true"> 
</receiver> 

Antwort

0
use service like : 

public class TestJobService extends JobService { 
    private static final String TAG = "SyncService"; 


    @Override 
    public boolean onStartJob(JobParameters params) { 
    // We don't do any real 'work' in this sample app. All we'll 
    // do is track which jobs have landed on our service, and 
    // update the UI accordingly. 
    Log.i(TAG, "on start job: " + params.getJobId()); 
    return true; 
    } 

    @Override 
    public boolean onStopJob(JobParameters params) { 
    Log.i(TAG, "on stop job: " + params.getJobId()); 
    return true; 
    } 

    MainActivity mActivity; 
    private final LinkedList<JobParameters> jobParamsMap = new LinkedList<JobParameters>(); 

    public void setUiCallback(MainActivity activity) { 
    mActivity = activity; 
    } 

} 
+0

Ich glaube, Sie mich vermissen verstehen, mein Problem ist die geplante Alarm läuft nicht, wenn app – Ali

+0

dann geschlossen ist Verwenden Sie es auf onDestroy() –

Verwandte Themen