在没有活动的引导启动服务 [英] Start Service at boot without activity

查看:185
本文介绍了在没有活动的引导启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个只包含一个服务(不活动)的应用程序。此服务必须在引导时启动。
我的问题是,它似乎在引导接收机不似乎调用如果没有活性。
我有下面的例子测试。
我有不同的文件:

I want create an application which contains only a service (no activity). This service must start on boot. My problem is that it seems the boot receiver don't seems call if there aren't activity. I have test with the following example. I have the different files :

MyReceiver.java:

MyReceiver.java :

package com.test.teststartserviceatboot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive( Context ctx, Intent i ) {

        Log.v( "MyReceiver", "onReceive : ");
        Intent intent = new Intent( ctx, MonService.class );
        ctx.startService(intent);
    }
}

MyService.java:

MyService.java :

package com.test.teststartserviceatboot;

import android.app.Service;

public class MyService extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v( "MyService","onStartCommand" );
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public IBinder onBind( Intent arg0 ) {
        Log.v( "MyService","onBind" );
        return null;
    }
}

MainActivity.java:

MainActivity.java:

package com.test.teststartserviceatboot;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

我只修改我的几个测试的Andr​​oidManifest。

I only modify the AndroidManifest on my several test.


  • 测试1(有活动)

<uses-sdk android:minSdkVersion="17" 
        android:targetSdkVersion="17"/>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver android:name=".MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>

- >重启后,服务running.I查看日志:

-> after reboot, the service is running.I see log :

AtivityManager开始PROC com.test.teststartserviceatboot广播com.test.teststartserviceatboot / .MyReceiver:PID = 1808 UID = 10156导报= {} 50156

AtivityManager Start proc com.test.teststartserviceatboot for broadcast com.test.teststartserviceatboot/.MyReceiver: pid=1808 uid=10156 gids={50156}

MyReceiver的onReceive结果
  为MyService onStartCommand

MyReceiver onReceive
MyService onStartCommand


  • 2的测试(不活动)

<uses-sdk android:minSdkVersion="17" 
        android:targetSdkVersion="17"/>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <service
        android:name=".MyService"
        android:exported="false"
        android:label="MyService" >
    </service>

    <receiver android:name=".MyReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>


- >服务没有运行

->The service don't running


  • 测试3

  • Test 3

我用测试1(有活动)相同appllication。这一次我杀之前重新启动平板电脑(与参数> Apps-> TestServiceAtBoot->强制停止)的应用程序。
- >重启后,服务不运行

I use the same appllication on test 1 (with activity). This time I kill the app before to restart the tablet (Parameter->Apps->TestServiceAtBoot->force stop). -> After reboot, service don't running

是否有必要对brodcast接收作品的活动?为什么?

Is it necessary to have an activity for brodcast receiver works ? And why ?

感谢您为您减轻。

推荐答案

在Android 3.1中,广播接收器将无法工作,直到用户手动发起的活动,这是提供安全保障。一旦用户运行首次应用那么你的广播接收器会一直运行,除了它不会强制停止它。一旦第一时间推出活动的广播接收器将运行,即使重新启动除冰后。

From Android 3.1, BroadcastReceiver will not work until the user has manually launched an activity, This is for provide security . once the user runs the app for the first time then your BroadcastReceiver will run always except it does not Force Stop it. Once activity launch at first time your broadcast receiver will run even after reboot your deice.

因此​​,在您的应用程序,你必须有一个活动运行的BroadcastReceiver。

Therefore in your application you must have one Activity to run BroadcastReceiver.

这篇关于在没有活动的引导启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆