正在后台尝试 运行 应用程序(Minimized/Screen Locked/Removed 来自 运行ning 应用程序)

Trying to run application in the background (Minimized/Screen Locked/Removed from running application)

我正在编写一个 android 应用程序,一旦检测到未接来电就会触发 SMS。到目前为止,我能够检测到未接来电并将短信发送给来电者。

如果我 运行 我的应用程序(打开 SWITCH(如智能开关)检查振铃电话并检测未接来电),然后有人在打电话,如果是未接来电,短信被正确触发。 如果我最小化我的应用程序(请勿锁定 phone)并打开 Instagram/Other 应用程序,那么 SMS 也会发送给来电者。

但是,

当我的应用程序(启用此功能的智能开关处于打开状态)时,但我关闭了我的应用程序,没有发送短信。 当我的应用程序最小化(屏幕锁定)时,没有消息被触发。 (智能开关开启) 当我的应用程序打开(屏幕锁定)时,没有消息被触发。 (智能开关开启)

我是 android 的新手。请帮助我,如果我的 SMART SWITCH 打开,我想保持我的应用程序 运行ning,我的应用程序将继续监视未接来电并向呼叫者发送短信,无论应用程序是否在后台,屏幕是否锁定只要我的智能开关打开。

首先你需要有关于任务的权限

<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

Services overview

我就是这样解决的。

public class myservices 扩展服务 { @覆盖 public int onStartCommand(Intent 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;
}

我尝试启动一个服务并且成功了,为了让后台工作,我们需要一个前台。

public class myservices extends Service

{ @覆盖 public int onStartCommand(Intent 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;
}