gpt4 book ai didi

android - 在删除联系人或短信时在没有 Activity 的情况下在后台运行应用程序提醒用户

转载 作者:行者123 更新时间:2023-11-29 22:09:38 25 4
gpt4 key购买 nike

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

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

}
}

最佳答案

为服务中的应用程序注册ACTION_SCREEN_ONACTION_USER_PRESENT 广播接收器,并在屏幕打开或用户出现时启动服务。您可以注册 ACTION_SCREEN_OFF 广播接收器以在手机屏幕关闭时停止服务,以避免您的应用耗尽电池电量。如:

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

广播接收器:

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
}

}
}

关于android - 在删除联系人或短信时在没有 Activity 的情况下在后台运行应用程序提醒用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9971597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com