使用Android的拨号盘启动应用程序拨号盘、应用程序、Android

2023-09-08 09:28:54 作者:彼岸ヾ荼靡花开

我想我用下面的code,推出通过拨号pad.I我的申请。对于拨号盘启动应用程序(在广播接收器)

I want to launch my application through dial pad.I am using the following code. For dial pad to launch application (in Broadcast receiver)

public class HiddenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
try{

               // Toast.makeText(context,"Number Dialed",1).show();

                Intent serviceIntent = new Intent(context,MainActivity.class);
                    serviceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(serviceIntent);



                }
                catch(Exception e)
                {
                    Log.d(TAG, ""+e.getMessage());
                }

虽然经过我现在要启动中,我用我的主要活动拨号盘pressing键下面的

While pressing key through dial pad I want to launch my main activity in which I used the following

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hidden_receiver); 

         //Intent call here
        Intent intent=getIntent();
        String message = intent.getStringExtra(MainActivity.TELEPHONY_SERVICE);
         //text here

但是,当我preSS我的code其拨打的号码消失,但既不是拨号盘消失,也不MainActivity启动。 这怎么能问题得到解决?帮我出..... 谢谢你。

But when I press my code its dialed number disappear but neither dialer pad disappear nor MainActivity launches. how can this issues be resolved?Help me out..... Thanks.

推荐答案

使用了的BroadcastReceiver 如下:

public class MyOutgoingCallHandler extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
     // Extract phone number reformatted by previous receivers
    String phoneNumber = getResultData();
    if (phoneNumber == null) {
      // No reformatted number, use the original
      phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    }

         if(phoneNumber.equals("1234")){ // DialedNumber checking.
        // My app will bring up, so cancel the broadcast
        setResultData(null);

        // Start my app
        Intent i=new Intent(context,MainActivity.class);
        i.putExtra("extra_phone", phoneNumber);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
         }

}

}

不要忘了在你的清单中注册该接收器

Don't forget to register this receiver in your manifest

    <receiver android:name="MyOutgoingCallHandler">
        <intent-filter >
            <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
        </intent-filter>
    </receiver>

此外,包括权限:

Also, include the permission:

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

您MainActivity里面现在,如果你忽略了一些检查接收器,你会得到所拨打的号码,

Now, if you are ignoring the number checking in receiver, you'll get dialed number inside your MainActivity,

String phone=getIntent().getStringExtra("extra_phone");
    if(!phone.equals(null)){
        Toast.makeText(getBaseContext(), phone, Toast.LENGTH_LONG).show();
    }