首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >尝试在后台运行应用程序(最小化/屏幕锁定/从正在运行的应用程序中删除)

尝试在后台运行应用程序(最小化/屏幕锁定/从正在运行的应用程序中删除)
EN

Stack Overflow用户
提问于 2020-06-18 04:29:14
回答 2查看 45关注 0票数 0

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

如果我运行我的应用程序(打开检查振铃呼叫并检测未接来电的开关(类似于智能开关)),然后有人呼叫,如果是未接来电,SMS将正确触发。如果我最小化我的应用程序(不要锁定手机)并打开Instagram/其他应用程序,那么SMS也会发送给呼叫者。

但,

当我的应用程序(启用此功能的智能开关打开),但我关闭我的应用程序,没有短信发送。当我的应用程序最小化(屏幕锁定)时,不会触发任何消息。(智能开关打开)当我的应用程序打开(屏幕锁定)时,不会触发任何消息。(智能开关打开)

我是android的新手。请帮助我,我想保持我的应用程序运行,如果我的智能开关打开,我的应用程序将继续监控未接来电并向呼叫者发出短信,而不管应用程序是否在后台,只要我的智能开关打开,屏幕锁定或没有。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-23 22:26:22

我试着启动一个服务,它工作了,为了让后台工作,我们需要一个前台。

代码语言:javascript
运行
复制
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);

代码语言:javascript
运行
复制
    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;
}
票数 0
EN

Stack Overflow用户

发布于 2020-06-18 04:46:21

首先您需要拥有任务的权限

代码语言:javascript
运行
复制
<uses-permission android:name="android.permission.WAKE_LOCK" />

代码示例

代码语言:javascript
运行
复制
 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

这就是我解决这个问题的方法。

公共类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);

代码语言:javascript
运行
复制
    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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62437840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档