在删除联系人或短信时,在没有活动警报的情况下在后台运行应用程序

时间:2012-04-02 06:08:52

标签: android

我想让我的应用程序在后台运行并倾听 联系,短信删除事件。 为此,我在我的应用程序中创建了一项服务,但我知道如何在没有活动的情况下开始 我的代码是这样的

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

}
}

3 个答案:

答案 0 :(得分:1)

服务只能通过活动,BroadCast接收器或已启动的服务启动 。它不能独立(它不能单独启动)。因此,您需要两个组件中的一个来启动它。你可以做一个开始服务的活动,这是首选方式。但是,如果您不想提供用户界面,请实施一个广播接收器,当手机开机并启动完成时,该接收器会启动,请在此内部启动您的服务。这也可以帮助您在手机启动后立即运行服务。 例如在你的清单中:

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

并在br:

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

}

答案 1 :(得分:1)

为您的应用服务注册ACTION_SCREEN_ONACTION_USER_PRESENT广播接收者,并在屏幕开启或用户在场时启动服务。您可以在手机屏幕关闭时注册ACTION_SCREEN_OFF广播接收器以停止服务,以避免您的app.as电池耗尽:

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

   }
}

答案 2 :(得分:0)

在您的活动中..将此代码放入oncreate

Intent svc=new Intent(youractivity.this,DeleteService.class);
            startService(svc);