在App未运行时使用AlarmManager运行服务

时间:2014-08-24 11:43:09

标签: android android-broadcast

我想每隔X次调度一次Service

在我的活动中,我创建了警报:

        // OnCreate()
        alarmIntent = new Intent ( this, AlarmReceiver.class );
        pendingIntent = PendingIntent.getBroadcast( this.getApplicationContext(), 1, alarmIntent, 0 );
        alarmManager = ( AlarmManager ) getSystemService( ALARM_SERVICE );
        alarmManager.setRepeating( AlarmManager.RTC_WAKEUP, ( 30 * 1000 ),( 30 * 1000 ), pendingIntent );

并在接收者中:

public class AlarmReceiver extends BroadcastReceiver {

    private static final String DEBUG_TAG = "AlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(DEBUG_TAG, "Recurring alarm; requesting download service.");

        // start the download
        Intent downloader = new Intent(context, MyService.class);
        context.startService(downloader);
    }

}

AndroidManifest.xml我注册:

 <receiver android:name="com.myapp. AlarmReceiver"></receiver>

仅当应用正在运行时才有效... ...如果用户关闭应用,我该如何安排它呢?

1 个答案:

答案 0 :(得分:0)

i found this code in internet and it works, may be  can help you.

package YourPackage;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.Toast;

public class Alarm extends BroadcastReceiver 
{    
     @Override
     public void onReceive(Context context, Intent intent) 
     {   
         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
         wl.acquire();

         // Put here YOUR code.
         Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example

         wl.release();
     }

 public void SetAlarm(Context context)
 {
     AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
     Intent i = new Intent(context, Alarm.class);
     PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
     am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000 * 60 * ,pi);
  }

 public void CancelAlarm(Context context)
 {
     Intent intent = new Intent(context, Alarm.class);
     PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
     alarmManager.cancel(sender);
 }
 }


 //Set Alarm from Service:


package YourPackage;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class YourService extends Service
{
    Alarm alarm = new Alarm();
    public void onCreate()
    {
        super.onCreate();       
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId){
         alarm.SetAlarm(YourService.this);
         return START_STICKY;
    }



public void onStart(Context context,Intent intent, int startId)
{
    alarm.SetAlarm(context);
}

@Override
public IBinder onBind(Intent intent) 
{
    return null;
}
}
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
...
<receiver android:name=".AutoStart">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
</receiver>




package YourPackage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AutoStart extends BroadcastReceiver
{   
    Alarm alarm = new Alarm();
    @Override
    public void onReceive(Context context, Intent intent)
    {   
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
        {
            alarm.SetAlarm(context);
        }
    }
}