Android BroadcastReceiver,设备重启后自动运行服务 [英] Android BroadcastReceiver, auto run service after reboot of device

查看:22
本文介绍了Android BroadcastReceiver,设备重启后自动运行服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在写一个应用程序,当手机重启时,服务将自动启动,而不是点击应用程序.

Hello i am writing an application, which is when the phone reboot, the service will auto start instead of click on the application.

这是我的代码

BootCompleteReceiver.java

package com.example.newbootservice;

import android.content.BroadcastReceiver;  
import android.content.Context;  
import android.content.Intent;    

public class BootCompleteReceiver extends BroadcastReceiver {   

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

        Intent service = new Intent(context, MsgPushService.class);  
        context.startService(service);   

    }  

}

MsgPushService.java

package com.example.newbootservice;

import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;   
import android.widget.Toast;

public class MsgPushService extends Service {  


    @Override  
    public void onCreate() {  
        super.onCreate();    
    }  

    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  

        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        return Service.START_STICKY;  
    }  

    @Override
    public void onDestroy() {

        super.onDestroy();
        Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
    }

    @Override  
    public IBinder onBind(Intent arg0) {  
        return null;  
    }  
} 

MainActivity.java(不确定我是否需要这个)

MainActivity.java (not sure whether i need this)

package com.example.newbootservice;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        startService(new Intent(getBaseContext(), MsgPushService.class));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.newbootservice"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service android:name=".MsgPushService"/>

        <receiver android:name=".BootCompleteReceiver">  
            <intent-filter>     
                <action android:name="android.intent.action.BOOT_COMPLETED"/>  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
        </receiver>

        <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>
    </application>
</manifest>

我希望服务在重启后自动启动,而不是手动启动.

I want the service to be started automatically after reboot instead of starting it manually.

推荐答案

先做这个

1) 在您的 元素中:

1) In your <manifest> element:

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

这篇关于Android BroadcastReceiver,设备重启后自动运行服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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