我正在编写一个android应用程序,将触发短信一旦未接电话被检测到。到目前为止,我能够检测未接来电并将SMS发送给呼叫者。
如果我运行我的应用程序(打开检查振铃呼叫并检测未接来电的开关(类似于智能开关)),然后有人呼叫,如果是未接来电,SMS将正确触发。如果我最小化我的应用程序(不要锁定手机)并打开Instagram/其他应用程序,那么SMS也会发送给呼叫者。
但,
当我的应用程序(启用此功能的智能开关打开),但我关闭我的应用程序,没有短信发送。当我的应用程序最小化(屏幕锁定)时,不会触发任何消息。(智能开关打开)当我的应用程序打开(屏幕锁定)时,不会触发任何消息。(智能开关打开)
我是android的新手。请帮助我,我想保持我的应用程序运行,如果我的智能开关打开,我的应用程序将继续监控未接来电并向呼叫者发出短信,而不管应用程序是否在后台,只要我的智能开关打开,屏幕锁定或没有。
发布于 2020-06-23 22:26:22
我试着启动一个服务,它工作了,为了让后台工作,我们需要一个前台。
public class myservices extends Service{ @Override public int onStartCommand( intent,int flags,int startId) { Intent notificationIntent = new Intent(this,MainActivity.class);//打开MainActivity onClick PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
Intent busy = new Intent(this, InterceptCall.class);
Intent driving = new Intent(this, InterceptCall.class);
Intent meeting = new Intent(this, InterceptCall.class);
busy.putExtra("mode","busy");
driving.putExtra("mode", "driving");
meeting.putExtra("mode", "meeting");
PendingIntent busyIntent = PendingIntent.getBroadcast(this, 1, busy, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
PendingIntent drivingIntent = PendingIntent.getBroadcast(this, 2, driving, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
PendingIntent meetingIntent = PendingIntent.getBroadcast(this, 3, meeting, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Smart Mode is on ")
.setContentText("Drag down to display modes.")
.setContentIntent(pendingIntent)
.setColor(Color.MAGENTA)
.setSmallIcon(R.drawable.pingedlogo)
.addAction(R.mipmap.ic_launcher, "Busy", busyIntent)
.addAction(R.mipmap.ic_launcher, "Driving", drivingIntent)
.addAction(R.mipmap.ic_launcher, "Meeting", meetingIntent)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}发布于 2020-06-18 04:46:21
首先您需要拥有任务的权限
<uses-permission android:name="android.permission.WAKE_LOCK" />代码示例
Thread(Runnable {
// a potentially time consuming task
val bitmap = processBitMap("image.png")
imageView.post {
imageView.setImageBitmap(bitmap)
}
}).start()此外,您还需要
我建议你看看下一页。
https://developer.android.com/guide/components/processes-and-threads
这就是我解决这个问题的方法。
公共类myservices扩展服务{ @Override public int onStartCommand( intent,int标志,int startId) { Intent notificationIntent = new Intent(this,MainActivity.class);//打开MainActivity onClick pendingIntent PendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
Intent busy = new Intent(this, InterceptCall.class);
Intent driving = new Intent(this, InterceptCall.class);
Intent meeting = new Intent(this, InterceptCall.class);
busy.putExtra("mode","busy");
driving.putExtra("mode", "driving");
meeting.putExtra("mode", "meeting");
PendingIntent busyIntent = PendingIntent.getBroadcast(this, 1, busy, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
PendingIntent drivingIntent = PendingIntent.getBroadcast(this, 2, driving, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
PendingIntent meetingIntent = PendingIntent.getBroadcast(this, 3, meeting, PendingIntent.FLAG_UPDATE_CURRENT); //For mini icon in notification bar
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Smart Mode is on ")
.setContentText("Drag down to display modes.")
.setContentIntent(pendingIntent)
.setColor(Color.MAGENTA)
.setSmallIcon(R.drawable.pingedlogo)
.addAction(R.mipmap.ic_launcher, "Busy", busyIntent)
.addAction(R.mipmap.ic_launcher, "Driving", drivingIntent)
.addAction(R.mipmap.ic_launcher, "Meeting", meetingIntent)
.build();
startForeground(1, notification);
return START_NOT_STICKY;
}https://stackoverflow.com/questions/62437840
复制相似问题