我有一个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
被多次调用。我只是想在触发拨出电话时调用它。
接收器按原样发射。但你需要验证电话状态。即挂断电话时不要启动活动
@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
}
在所有操作之前:请确保你写了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); }
和: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);
希望有所帮助