1

i want to make my app to be run in background and listens for contact,sms deletion events. for that i created a service in my app but i dnt how to start without activity my code is like this

public class DeleteService extends Service {

ContentResolver cr;

 MyContentObserver observer=new MyContentObserver();
@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return mBinder;
}
@Override
public void onCreate() {

     cpath=ContactsContract.Contacts.CONTENT_URI;

                // some action
        }

@Override

public void onDestroy() {

}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Launch a background thread to do processing.
    super.onStartCommand(intent, flags, startId);
    cpath=ContactsContract.Contacts.CONTENT_URI;
    cr=getContentResolver();
    cur=cr.query(cpath, null, null, null, null);


    this.getApplicationContext().getContentResolver().registerContentObserver(cpath, true, observer);
    return Service.START_STICKY;

    }
 private class MyContentObserver extends ContentObserver {

        public MyContentObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            nfm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
            int NOTIFICATION_ID = 1;
       Intent intent1 = new Intent();
       PendingIntent pi = PendingIntent.getActivity(DeleteService.this, 1, intent1, 0);
       nf=new Notification(R.drawable.ic_launcher,"Contact Database changed",System.currentTimeMillis());
       nf.setLatestEventInfo(getApplicationContext(), "Delete Event", "contact name", pi);
       nf.flags = nf.flags |
             Notification.FLAG_ONGOING_EVENT;
    }
        @Override
        public boolean deliverSelfNotifications()
        { 
             super.deliverSelfNotifications();
            return true;

        }
}
 public class LocalBinder extends Binder {
        DeleteService getService() {
            return DeleteService.this;
        }

}
}
Mayank
  • 8,777
  • 4
  • 35
  • 60
jagadeesh
  • 49
  • 5

3 Answers3

1

A service can only by started by an Activity, or a BroadCast receiver, or a service which is already started. It can't be stand-alone(It can't start by itself). So, you would need one of the two components to start it. you can make an activity which starts the service which is the preferred way. But if you don't want to provide a user interface, implement a broadcast receiver which fires up when the phone is switched on and the boot up is completed, Inside that br, start your service. This will also help you run the service as soon as a phone starts. for example in your manifest:

 <receiver android:name="com.my.MyBroadcastReceiver">  
    <intent-filter>  
        <action android:name="android.intent.action.BOOT_COMPLETED" />  
    </intent-filter>  
</receiver>

and in the br:

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i=new Intent(context,DeleteService.class);
            context.startService(i);
     }

}

Akhil
  • 13,888
  • 7
  • 35
  • 39
1

register ACTION_SCREEN_ON or ACTION_USER_PRESENT broadcast recivers for your Appliction in Service and start Service when screen is on or user is present. you can register ACTION_SCREEN_OFF broadcast reciver for stoping Service when phone screen is off to avoid battery drain by your app.as:

In manifest.xml:

<receiver android:name="com.my.AppStart">  
    <intent-filter>  
        <action android:name="android.intent.action.SCREEN_ON" />  
        <action android:name="android.intent.action.SCREEN_OFF" /> 
        <action android:name="android.intent.action.USER_PRESENT" /> 
    </intent-filter>  
</receiver>

BroadcastReceiver :

public class AppStart extends BroadcastReceiver {
public static final String present = "android.intent.action.USER_PRESENT";
public static final String screenon = "android.intent.action.SCREEN_ON";
public static final String screenoff = "android.intent.action.SCREEN_OFF";
    @Override
    public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals(present) || intent.getAction().equals(screenon) )
    {
        Intent i=new Intent(context,DeleteService.class);
        context.startService(i);
    }
    if (intent.getAction().equals(screenoff))
    {
       //STOP YOUR SERVICE HERE
    }

   }
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • your code is definitely more battery-friendly.I didnt know about these intents.Thanks for telling about these intents. But if some other app. is modifying contacts programatically while the user screen is off, while the breceiver fire?] – Akhil Apr 02 '12 at 07:31
  • Please note that the `SCREEN_ON`, `SCREEN_OFF` intent filters seemingly will not work if only registered in the manifest. The `USER_PRESENT` filter will work if a lock-screen is present in the version of Android you're running. I'm pretty sure 4.x works fine with `USER_PRESENT` on most devices. See this other SO reference: http://stackoverflow.com/questions/3601014/can-an-action-user-present-broadcastreceiver-not-be-registered-in-the-manifest – treejanitor Jul 13 '13 at 16:54
0

In your activity .. put this code in oncreate

Intent svc=new Intent(youractivity.this,DeleteService.class);
            startService(svc);
5hssba
  • 8,079
  • 2
  • 33
  • 35