我正在尝试为我的每个呼叫事件设置模板,即未接呼叫、来电结束和呼出结束
我参考了一些代码在互联网上,并想出了这个。现在我收到呼出的联系号码,但有问题的来电联系号码。我没有收到任何价值。
我在这段代码中做错了什么?请帮助我
机器人菜单:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.
</intent-filter>
</receiver>
PhoneStateReceiver.java :
public abstract class PhoneStateReceiver extends BroadcastReceiver {
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber;
protected Context savedContext;
@Override
public void onReceive(Context context, Intent intent) {
String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){
//number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
//Toast.makeText(context,number,Toast.LENGTH_LONG).show();
}else{
//number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
//Toast.makeText(context,number,Toast.LENGTH_LONG).show();
}
//We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number.
if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
}else{
String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
//String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
int state = 0;
if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
state = TelephonyManager.CALL_STATE_IDLE;
//number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
state = TelephonyManager.CALL_STATE_OFFHOOK;
//number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
state = TelephonyManager.CALL_STATE_RINGING;
//number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
onCallStateChanged(context, state, number);
}
}
//Derived classes should override these to respond to specific events of interest
protected void onIncomingCallStarted(Context ctx, String number, Date start){}
protected void onOutgoingCallStarted(Context ctx, String number, Date start){}
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){}
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end){}
protected void onMissedCall(Context ctx, String number, Date start){}
//Deals with actual events
//Incoming call- goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up
//Outgoing call- goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up
public void onCallStateChanged(Context context, int state, String number) {
if(lastState == state){
//No change, debounce extras
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
callStartTime = new Date();
savedNumber = number;
onIncomingCallStarted(context, savedNumber, callStartTime);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing done on them
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
callStartTime = new Date();
onOutgoingCallStarted(context, savedNumber, callStartTime);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//Went to idle- this is the end of a call. What type depends on previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
//Ring but no pickup- a miss
onMissedCall(context, savedNumber, callStartTime);
}
else if(isIncoming){
onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
}
else{
onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
}
break;
}
lastState = state;
}
}
CallReceiver.java
public class CallReceiver extends PhoneStateReceiver {
SharedPreferences sharedPreferences;
@Override
protected void onIncomingCallStarted(Context context, String number, Date start) {
//openDialog(context, number);
}
@Override
protected void onOutgoingCallStarted(Context context, String number, Date start) {
//openDialog(context, number);
//sharedPreferences = context.getSharedPreferences("user_sms_pref", Context.MODE_PRIVATE);
//SmsManager smsManager = SmsManager.getDefault();
//Toast.makeText(context,number + " "+sharedPreferences.getString("outgoingCall", " "),Toast.LENGTH_LONG);
//smsManager.sendTextMessage(number,null,sharedPreferences.getString("outgoingCall", " "),null,null);
}
@Override
protected void onIncomingCallEnded(Context context, String number, Date start, Date end) {
sharedPreferences = context.getSharedPreferences("user_sms_pref", Context.MODE_PRIVATE);
openDialog(context, number);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number,null,sharedPreferences.getString("incomingCall", " "),null,null);
}
@Override
protected void onOutgoingCallEnded(Context context, String number, Date start, Date end) {
openDialog(context, number);
sharedPreferences = context.getSharedPreferences("user_sms_pref", Context.MODE_PRIVATE);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number,null,sharedPreferences.getString("outgoingCall", " "),null,null);
}
@Override
protected void onMissedCall(Context context, String number, Date start) {
openDialog(context, number);
sharedPreferences = context.getSharedPreferences("user_sms_pref", Context.MODE_PRIVATE);
SmsManager smsManager = SmsManager.getDefault();
Toast.makeText(context,number,Toast.LENGTH_LONG);
smsManager.sendTextMessage(number,null,sharedPreferences.getString("missedCall", ""),null,null);
}
private void openDialog(Context context, String number) {
Intent i = new Intent(context, PhoneIncomingActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("Number",number);
context.startActivity(i);
}
}
1条答案
按热度按时间w8f9ii691#
**
android.permission.PROCESS_OUTGOING_CALLS
**已弃用,这也将属于Google在API 28中限制的CallLog
权限组。因此,在API 28及以后的版本中,您不会得到“否”。