如何从CallScreeningService在Java中启动活动

huangapple go评论69阅读模式
英文:

How to start activity from CallScreeningService in Java

问题

我正在尝试使包含此CallScreeningService的应用在传入的电话号码与特定号码匹配时打开。在这种情况下,onScreenCall中的if语句被调用,但未启动Activity。我不太确定原因,我猜想可能是因为我没有正确的应用程序上下文。有人知道如何获取正确的上下文,或者我在这里做错了什么吗?

public class CallScreenService extends CallScreeningService {
    Context nContext = this; 

    @Override
    public void onScreenCall(Call.Details callDetails) {

       if (callDetails.getHandle().toString().equals("tel:333333333")) {

            Intent i = new Intent(nContext, MainActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            nContext.startActivity(i);
        
       } 
    }
}
英文:

I'm trying to make the application that this CallScreeningService is written in open when the incoming phone number matches certain numbers. In this case the if statement in onScreenCall calls runs but the Activity isn't started. I'm not sure why, I'm guessing it's because I don't have the right context of the application. Does anyone know how I would get the correct context or what i'm doing wrong here?

public class CallScreenService extends CallScreeningService {
Context nContext = this; 

@Override
public void onScreenCall(Call.Details callDetails) {

   if (callDetails.getHandle().toString().equals("tel:333333333")) {

        Intent i = new Intent(nContext, MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        nContext.startActivity(i);
    
   } 
}
}

答案1

得分: 1

应用程序需要在Android 10+上由用户授予SYSTEM_ALERT_WINDOW权限。

您需要将其添加到清单中:

并向用户请求:

// 向用户显示警告对话框,说明需要另外的权限
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(myIntent);

英文:

The app needs to have granted SYSTEM_ALERT_WINDOW permission by the user on Android 10+.

You have to add it in manifest:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

And to request it from user:

// Show alert dialog to the user saying a separate permission is needed
Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(myIntent);

huangapple
  • 本文由 发表于 2020年8月21日 00:58:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63509860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定