0

Possible Duplicate:
Trying to start a service on boot on Android

BroadcastReceiver

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;
    }
}

I would like the application to start the compareSIM service upon boot up, during boot up, this service will run as the current attached SIM card IMSI will be retrieved and matched with the already saved IMSI, once they are different the user will be brought to a login layout. I want to perform this during boot up but failed to do so... Kindly advice me on the coding, thanks

Community
  • 1
  • 1
Android_Rookie
  • 509
  • 2
  • 10
  • 25

3 Answers3

2

floow these steps for stating your service on BOOT:

Step 1: In AndroidManifest.xml add BOOT_COMPLETED permission as:

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

Step 2: In AndroidManifest.xml Register your Reciver as:

<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>

Step 3: In AndroidManifest.xml Register your Service as:

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

Step 3: In StartActivityAtBoot Start your service as:

    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();
        }
    }
}

This is all about Starting a Service on Boot.Thanks

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

You need to register the BroadcastReceiver in the Android manifest, like this:

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

Also make sure that you have this permission in the manifest:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • I did it already in the manifest – Android_Rookie May 21 '12 at 16:56
  • And does your BroadcastReceiver get run on device BOOT? Add some debug logging there and check the logcat to see if it is being run. Check also if your service is getting started. How far does it get? Any errors in the logcat? – David Wasser May 21 '12 at 17:40
0

Check Your androidManifest file. You need to add receiver at androidManifest file.

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