How to set intent filter for dial action in android?

◇◆丶佛笑我妖孽 提交于 2021-02-07 09:58:44

问题


I am trying to create a simple dial pad. I created all that and now I like to receive action dial intent from other applications.

<intent-filter>

       <action android:name="android.intent.action.DIAL" />

       <category android:name="android.intent.category.DEFAULT" />

       <data android:xxxxxx="XXXXXX" />

</intent-filter>

and now I don't know what should be filled with the data in action filter. Can anyone help me ? Thanks in advance.


回答1:


This is the intent filter should be used.

<intent-filter>
<action android:name="android.intent.action.CALL_DIAL" />                
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" />
</intent-filter>

And we can read the phone number from onCreate methord like this.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //start reading here
        Intent intent = getIntent();
        if(intent.getData()!=null){
            Log.d("intent received",intent.getData().toString());
            String phoneNumber = intent.getData().toString(); //contains tel:phone_no
            phoneNumber = phoneNumber.substring(4);
            Log.d("intent received","Received phone number : "+phoneNumber);
                  /// do what you like here
        }else{
            Log.d("intent received","null intent received");
        }
}



回答2:


The accepted answer is not working for me. After some search, I found this example from https://github.com/jdunck/Examples/blob/master/SimpleDialer/AndroidManifest.xml

<activity android:name=".DialerActivity" android:label="@string/title_dialer">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.DIAL" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.CALL_BUTTON" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
   <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:mimeType="vnd.android.cursor.dir/calls" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <action android:name="android.intent.action.DIAL" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="tel" />
    </intent-filter>
</activity>

This worked fine for me.




回答3:


<intent-filter> 

        <action android:name="android.intent.action.CALL" />
       <data android:scheme="tel" />
       <category android:name="android.intent.category.DEFAULT" />
       <action android:name="android.intent.action.CALL_PRIVILEGED" />

    </intent-filter>

in java

Uri number = Uri.parse("tel:"  + edit_text_number);
Intent i = new Intent(Intent.ACTION_DIAL, number);
startActivity(i);



回答4:


You have to add these intent filter -

<intent-filter>
<action android:name="android.intent.action.CALL_PRIVILEGED" />                
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="tel" />
</intent-filter>

Also the most important thing, to add permission -

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

Cheers :)



来源:https://stackoverflow.com/questions/27688063/how-to-set-intent-filter-for-dial-action-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!