I want to activate a service from my broadcastReceiver, this is my code but it didn’t work:
public class PackageChangeReceiver extends BroadcastReceiver {
Context context;
Deletecontact delete= new Deletecontact();
@Override
public void onReceive(Context ctx, Intent intent) {
Uri uri = intent.getData();
String pkg = uri != null ? uri.getSchemeSpecificPart() : null;
if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")&& pkg.equals("com.alarm"))
{Log.i("action","the package is removed");
Intent service = new Intent( context, Deletecontact.class);
context.startService(service);
}}}
and this is the service
public void onCreate()
{
//deletecontact();
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
return START_STICKY;
//return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
return mBinder;
}
public class MyBinder extends Binder {
Deletecontact getService() {
return Deletecontact.this;
}
}
I just want to call the service when the action of the broadcast is set
@Emna when i see you code for start service you have used this code to start service.
In this code
contextthis not assign like belowSo before you call
startServiceAdd above code after
onReceive.And Make sure that you have define
Deletecontact.classthis class as Service Tag in AndroidManifest.xml For Example Below :Hope this will work .