Android启动服务而不启动应用程序? [英] Android start service without starting application?
本文介绍了Android启动服务而不启动应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在不启动应用程序的情况下启动服务?我需要至少每小时刷新一次应用程序数据,如果应用程序中没有用户,怎么办?
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?
推荐答案
您可以在启动时启动服务. 您的AndroidManifest.xml文件中需要以下内容:
you can start your service on boot. You need the following in your AndroidManifest.xml file:
1)在您的元素中:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2)在您的元素中(一定要为您的BroadcastReceiver使用完全限定的[或相对]类名称):
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>
在MyBroadcastReceiver.java中:
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);
}
}
这篇关于Android启动服务而不启动应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文