无需用户交互即可在后台运行Android应用

时间:2014-08-14 11:04:11

标签: java android

我有一个Android应用程序,每5分钟不断更新日志。

我使用警报管理器来实现此目的。

但该应用程序会自动打开和关闭。

有没有办法在后台执行此操作?

2 个答案:

答案 0 :(得分:3)

您可以使用Android服务执行此任务。

http://developer.android.com/guide/components/services.html

答案 1 :(得分:2)

首先,您必须使用“服务:http://developer.android.com/guide/components/services.html

接下来,您可以在BOOT事件的后台启动您的程序:

public class BootReciever extends BroadcastReceiver {   

    @Override
    public void onReceive(Context context, Intent intent) {

     Intent myIntent = new Intent(context, LogService.class);
     context.startService(myIntent);
    }

在你的清单中声明theese:

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

  <!-- Declaring broadcast receiver for BOOT_COMPLETED event -->
        <receiver android:name=".BootReciever" android:enabled="true" android:exported="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
相关问题