Android start service without starting application

2019-02-20 15:24发布

Is it possible to start service without starting application? I need to refresh data of my application at-least hourly, how can I do it if user no in my application?

2条回答
We Are One
2楼-- · 2019-02-20 16:09

you can start your service on boot. You need the following in your AndroidManifest.xml file:

1) In your element:

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

2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):

<receiver android:name="com.example.MyBroadcastReceiver">  
<intent-filter>  
    <action android:name="android.intent.action.BOOT_COMPLETED" />  
</intent-filter>  

In MyBroadcastReceiver.java:

  package com.example;

    public class MyBroadcastreceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
  }
查看更多
戒情不戒烟
3楼-- · 2019-02-20 16:25

Yes it is possible.

You can read about different methods of doing so here.

查看更多
登录 后发表回答