Android服务需要在evey十分钟内持续运行

时间:2016-03-31 07:55:30

标签: android

即使应用程序已关闭(已终止),或者即使用户未启动应用程序,我也希望服务正在运行。我希望在安装应用程序后启动该服务,从这一点来说,该服务需要每十分钟运行一次。

虽然我找到了一些解决方案,例如

这些对我不起作用,因为我是Android编程的新手。 如果有任何样本工作源代码,请向我提供如何开发它的信息。

这是我的服务类..

public class MyService extends Service {

    MyReceiver receiver = new MyReceiver();

    public void onCreate()
    {
        super.onCreate();       
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

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

    @Override        
    public void onStart(Intent intent, int startId)
    {
        receiver.SetAlarm(this);
    }

}

这是我的Receiver Class ..

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            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();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

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

}

我在AndroidManifest.xml文件中添加了服务和接收器。

<uses-permission android:name="android.permission.WAKE_LOCK" />

<service android:name=".MyService" 
            android:exported="false" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </service>

 <receiver android:name=".MyReceiver" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>

我在主要活动中添加了以下代码。

Intent service = new Intent(getApplicationContext(), MyService.class);
        this.startService(service);

2 个答案:

答案 0 :(得分:1)

Android OS将在您安装申请表格游戏商店时通知。

1。清单文件中添加接收器,该文件将通知您用户已安装应用程序:

<receiver android:name="com.mypackagename.Installreceiver" android:exported="true">
      <intent-filter>
          <action android:name="com.android.vending.INSTALL_REFERRER" />
     </intent-filter>
</receiver>

2。启动服务将从Installreceiver广播接收器每10分钟继续运行。

public class Installreceiver extends BroadcastReceiver{
       @Override
        public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context.getApplicationContext(), YourService.class));
       }
    }

YourService.java

public class YourService extends Service {

    private static String TAG = "MyService";
    private Handler handler;
    private Runnable runnable;
    private final int runTime = 10000;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate");

        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {

                handler.postDelayed(runnable, runTime);
            }
        };
        handler.post(runnable);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
}

更新1:

3。如果操作系统终止您的服务,那么您需要通过另一个接收器重新开始。

在服务中添加以下两种方法:

@Override
public void onTaskRemoved(Intent rootIntent) {
   super.onTaskRemoved(rootIntent);
   sendBroadcast(new Intent("IWillStartAuto"));
}

 @Override
 public void onDestroy() {
   super.onDestroy();
   sendBroadcast(new Intent("IWillStartAuto"));
 }

4. manifest中添加接收器

<receiver android:name=".RestartServiceReceiver" >
     <intent-filter>
         <action android:name="IWillStartAuto" >
         </action>
      </intent-filter>
 </receiver>

5. 添加接收器

public class RestartServiceReceiver extends BroadcastReceiver{
   @Override
    public void onReceive(Context context, Intent intent) {
    context.startService(new Intent(context.getApplicationContext(), YourService.class));
   }
}

更新2:

6。在清单中添加权限。

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

7. 在清单中添加接收器。

<receiver android:name=".BootCompletedReceiver" >
      <intent-filter>
             <action android:name="android.intent.action.BOOT_COMPLETED" />
             <action android:name="android.intent.action.QUICKBOOT_POWERON" />
      </intent-filter>
</receiver>

7。 BootCompletedReceiver,java:

public class BootCompletedReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent arg1) {
    context.startService(new Intent(context.getApplicationContext(), YourService.class));
  }
}

希望这会对你有所帮助。

答案 1 :(得分:0)

嗯,你已经在你的问题中得到了答案,你只需要尝试那些答案。 如果你仍然无法做到,那么在这里发布您的代码。这就是Stack溢出的工作原理。

  

FYI使用AlarmManager间隔10分钟:)