【问题标题】:launching application through dialer pad通过拨号盘启动应用程序
【发布时间】:2013-11-13 06:39:26
【问题描述】:

我正在学习通过拨号盘启动我的应用程序。我正在使用以下代码。 用于拨号盘启动应用程序 (在广播接收器中)

public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
           Bundle bundle = intent.getExtras();
            if (null == bundle)
                return;

            String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
                //here change the number to your desired number
                String compare_num="5556";
                if(phoneNumber.equals(compare_num))
                {
                    setResultData(null);
                //  Guardian.changeStealthMode(context,PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
                    //Intent to move to next Activity
                    Intent myintent=new Intent(context,MainActivity.class);
                    myintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(myintent);
                   // abortBroadcast();

以及包含主要活动的启动应用

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

         //Intent call here
        Intent intent=getIntent();
        String message = intent.getStringExtra(MainActivity.TELEPHONY_SERVICE);
         //text here
        EditText et = (EditText) findViewById(R.id.editText1);

     //Button here
        Button Login = (Button) findViewById(R.id.button1);
    }

当我启动此代码时,我的应用程序不会通过拨号盘启动。 请帮帮我.........

【问题讨论】:

  • 你是如何得到用户在拨号器上输入的号码的?
  • 只是通过手机拨号盘按数字.....是否需要任何代码?
  • 你可以使用android密码功能..
  • @DeepSan 抱歉,对此一无所知。请解释一下?

标签: android android-intent android-activity broadcastreceiver


【解决方案1】:

Android 类-

public class SecretCodeReceiver extends BroadcastReceiver {


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

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

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



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

}

}

Android 清单-

<receiver android:name=".SecretCodeReceiver"   android:enabled="true"
        >
        <intent-filter>


     <action android:name="android.provider.Telephony.SECRET_CODE" />
      <data android:scheme="android_secret_code" android:host="111" />


        </intent-filter>
    </receiver>

用户何时拨号

*#*#111#*#*

然后将触发所需的事件。如果您需要在拨号时触发事件,则需要按照“Shyam”的回答中提到的实现ACTION_NEW_OUTGOING_CALL

【讨论】:

【解决方案2】:

试试这个

public class Example extends BroadcastReceiver 
    {

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

          if (intent.getAction().equals(android.intent.action.NEW_OUTGOING_CALL)) {
           String phoneNumber = intent.getExtras().getString( android.intent.extra.PHONE_NUMBER );

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

          }

        }

    } 
in your manifest file 


<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>

【讨论】:

  • 你为它写广播接收器。
  • 除了这个broadcastReceiver吗?如果有的话请建议代码
  • 当我使用此代码时,它在按数字时没有响应。是否需要更多代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
相关资源
最近更新 更多