问题
my requirement is to launch an hidden app when a secret code is dailed.
MainActivity.java
public class MainActivity extends
BroadcastReceiver {
String dialed_number;
@Override
public void onReceive(Context context, Intent intent)
{
dialed_number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if(dialed_number.equals("*0*1235#"))
{
Intent appIntent = new Intent(context, MainActivity.class);
appIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(appIntent);
setResultData(null);
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tuto.bala.helloworld" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name=".MainActivity">
</receiver>
</application>
</manifest>
When i run the project I get the following exception: connection problem invalid mmi code android
Can anyone please help
Regards, Bala
回答1:
I have used same code it's working when we are given dial number is a number not to include *,#. and in Manifest file we have to declare the receiver like this
<receiver android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
and write permission as <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
Just see the below edited code:
Use the BroadcastReceiver as follows:
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:
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();
}
If you want to launch app as call to magic number, it's quite simple using BroadcastReceivers for outgoing call, you can get solution from Right Number app
回答2:
exception: connection problem invalid mmi code android
To detect outgoing phone call event you should register broadcast with NEW_OUTGOING_CALL
Action in AndroidManifest.xml
:
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
and also add PROCESS_OUTGOING_CALLS
permission :
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
来源:https://stackoverflow.com/questions/28188252/launch-hidden-app-when-secret-code-is-dialed