Android 开机启动, service 自动运行。

1. First of all, you need to declare the permission in AndroidManifest.xml

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

2. register broadcast receiver in AndroidManifest.xml

When android system finished startup, the Receiver will be called.

        <receiver android:name="com.project.pedometer.OnBoot">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

3. Implement the Receiver class 
public class OnBoot extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean needBoot = prefs.getBoolean("pedometer", false);

        if (needBoot) {
            Intent stepsIntent = new Intent(context,
                    StepService.class);
            context.startService(stepsIntent);
        }
    }
}


阅读更多
个人分类: Android Service
上一篇Solving the Android image loading problem: Volley vs. Picasso
下一篇apk 反编译工具的使用
想对作者说点什么? 我来说一句

android开机启动service

2010年09月17日 42KB 下载

没有更多推荐了,返回首页

关闭
关闭