如何在设备启动时启动服务?

时间:2012-05-21 16:43:03

标签: android service broadcastreceiver

  

可能重复:
  Trying to start a service on boot on Android

广播接收器

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

public class StartActivityAtBoot extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
            Intent i = new Intent(context, CompareIMSI.class);
            context.startService(i);
        }
    }
}

CompareSIM.java

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.widget.Toast;

public class CompareIMSI extends Service{

    Context context;
    TelephonyManager operator;

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

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
        //compareSIM();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
        compareSIM();
    }

    public void compareSIM(){

        final String STORAGE = "Storage";
        SharedPreferences unique = getSharedPreferences(STORAGE, 0);
        final String storedIMSI = unique.getString("simIMSI", "");
        final String currentIMSI = getSubscriberId().toString();

        if (!storedIMSI.equals(currentIMSI)){
            Intent i = new Intent(CompareIMSI.this, ScreenLockActivity.class);
            startActivity(i);
        }
    }

    public String getSubscriberId(){

        String IMSI = null;
        String serviceName = Context.TELEPHONY_SERVICE;
        TelephonyManager m_telephonyManager = (TelephonyManager) getSystemService(serviceName);
        IMSI = m_telephonyManager.getSubscriberId();
        return IMSI;
    }
}

我希望应用程序在启动时启动compareSIM服务,在启动期间,此服务将运行,因为当前连接的SIM卡IMSI将被检索并与已保存的IMSI匹配,一旦它们不同,用户将被带到登录布局。我想在启动时执行此操作但未能这样做...请告知我编码,谢谢

3 个答案:

答案 0 :(得分:2)

了解这些步骤,以便在BOOT上陈述您的服务:

第1步:在AndroidManifest.xml中添加BOOT_COMPLETED权限:

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

第2步:在AndroidManifest.xml中将Reciver注册为:

<receiver android:name=".StartActivityAtBoot" android:label="@string/app_name"> 
    <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</receiver>

第3步:在AndroidManifest.xml中注册您的服务:

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

第3步:在StartActivityAtBoot中启动您的服务:

    public class StartActivityAtBoot extends BroadcastReceiver
{
    static final String ACTION = "android.intent.action.BOOT_COMPLETED";
    public void onReceive(Context context, Intent intent) 
    {
        if (intent.getAction().equals(ACTION)) 
        {
                  context.startService(new Intent(context, 
                  CompareIMSI.class), null);
             Toast.makeText(context, "CompareIMSI service has started!", Toast.LENGTH_LONG).show();
        }
    }
}

这是关于在Boot.Thanks上启动服务的全部内容

答案 1 :(得分:0)

您需要在Android清单中注册BroadcastReceiver,如下所示:

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

还要确保您在清单中拥有此权限:

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

答案 2 :(得分:0)

检查您的androidManifest文件。你需要在androidManifest文件中添加接收器。

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