这是我到目前为止,但没有任何反应,当我输入拨号这个组合
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"/>
你也可能会发现这些有用的..
- http://android.programmerguru.com/simple-dialer-application/
- http://tikuflower.blogspot.com/2011/12/android.html
在接收器获取在所有广播? 如果没有,也许你忘了包括PROCESS_OUTGOING_CALLS许可。
据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");
这种变化至少对我的作品...
试试这个,
添加到这个清单中,这里的主机是12456,所以你的密码是*#*#123456#*#*
(拨号dialped)
<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);
}
}
文章来源: Android. Launch app from Dialer