繁体   English   中英

安卓。 从拨号器启动应用程序

[英]Android. Launch app from Dialer

到目前为止,这是我所拥有的,但是当我在拨号器中输入此组合时没有任何反应

public class DialReceiver extends BroadcastReceiver
{
    @Override
  public void onReceive(Context context, final Intent intent) {

    if (intent.getAction().equals(android.content.Intent.ACTION_NEW_OUTGOING_CALL)) {
        String phoneNumber = intent.getExtras().getString( android.content.Intent.EXTRA_PHONE_NUMBER );

        if(phoneNumber.equals("*#588637#")) { 
            Intent intent1 = new Intent(context , Activity.class);
            intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
            context.startActivity(intent1);
        }

    }

}
}

并在androidmanifest中

    <receiver
        android:name=".receiver.DialReceiver"
        android:exported="true"
        android:process=":background"
        tools:ignore="ExportedReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

尝试这些小变化..

String phoneNumber = intent.getExtras.getString("Intent.EXTRA_PHONE_NUMBER");

             if(phoneNumber.equals("*#588637#")) { 
             //do your stuff
             }

并且不要忘记在您的 Manifest.xml 文件中添加这一行

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

您也可能会发现这些有用..

接收器是否收到广播? 如果没有,也许您忘记包含PROCESS_OUTGOING_CALLS权限。

尝试这个,

在清单中添加这个,这里的主机是 12456,所以你的密码是*#*#123456#*#* (拨入)

<receiver android:name=".Dialer"> //here is your broadcast receiver class
        <intent-filter>
            <action android:name="android.provider.Telephony.SECRET_CODE" />
            <data android:scheme="android_secret_code"
                  android:host="123456"
            />
        </intent-filter>
    </receiver>

这是您的广播接收器类:

 class Dialer : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
         // Declare Here your launcher activity in Intent
        var  i : Intent = Intent(context, MainActivity::class.java)  
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context!!.startActivity(i);
    }
}

根据ridoy的第二个链接,

http://tikuflower.blogspot.com/2011/12/android.html

它应该是

String phoneNumber = intent.getStringExtra("android.intent.extra.PHONE_NUMBER");

而不是

String phoneNumber = intent.getExtras.getString("Intent.EXTRA_PHONE_NUMBER");

这种变化至少对我有用......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM