我有receiver
收听拨出电话
<receiver android:name="com.example.playground.OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<activity
android:name="com.example.playground.OutgoingCallActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_outgoing_call"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="com.example.playground.OutgoingCallActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
关注permission
s
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
我的receiver
:
package com.example.playground;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OutgoingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Intent contactIntent = new Intent("com.example.playground.OutgoingCallActivity");
contactIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
contactIntent.putExtra("number", number);
context.startActivity(contactIntent);
}
}
我的activity
:
Intent intent = getIntent();
String number = intent.getStringExtra("number");
displayContact(number);
public void displayContact(String number) {
contactsContainer = (TextView) findViewById(R.id.fullscreen_content);
contactsContainer.setText(number);
}
现在,当我拨打电话时,activity
被正确调用,但当我挂断电话时,activity
再次启动,我的receiver
被多次呼叫。我只是想在触发拨出电话时调用它。
答案 0 :(得分:0)
接收器按原样发射。但你需要验证电话状态。即挂断电话时不要启动活动
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)
&& intent.getStringExtra(TelephonyManager.EXTRA_STATE)
.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
return; // Dont start Activity
}
// Your code
}
答案 1 :(得分:0)
在进行所有操作之前:请确保您已经写过unregisterReceiver
,我遇到了同样的问题,因为我忘记了在活动中调用此方法,我不知道为什么,但是当我将其添加到我的活动中时编写一切都很好的代码
在您的活动中:
输入此属性:
private Debug_bluetooth_receiver addRemoveModulesDebugReceiver;
和:
@Override
protected void onResume() {
super.onResume();
addRemoveModulesDebugReceiver = new Debug_bluetooth_receiver();
registerReceiver(addRemoveModulesDebugReceiver, new IntentFilter("DEBUG_BLUETOOTH")); }
和:
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(addRemoveModulesDebugReceiver); }
and:OnResume之后
public class Debug_bluetooth_receiver extends BroadcastReceiver {
public Debug_bluetooth_receiver() { super(); }
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("DEBUG_BLUETOOTH")){
// some extras in Intent
boolean enableDebugBluetoothModule = intent.getBooleanExtra("STATE", true);
if (enableDebugBluetoothModule){
// TODO
}else {
// TODO
}
}
}
}
现在转到您要向此活动发送消息的另一个代码:
Intent intent = new Intent("DEBUG_BLUETOOTH");
intent.putExtra( "STATE", false); // send Extras
getActivity().sendBroadcast(intent);
希望有帮助