我想创建一个基于来电的Android应用。
当重要人士打电话时,我应该接听来电,但是如果其他人拨打我的电话应该保持沉默。
因为我是android的新手,所以对我来说非常困难。
答案 0 :(得分:4)
实现您的要求可以有四个步骤。
获取来电号码
您需要创建一个Broadcast receiver
说CallReceiver
在Manifest中分配其操作。
<receiver android:name=".CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
在该广播中,您需要将PhoneStateListener
添加到您的TelephonyManager,PhoneStateListener,并使用Number参数覆盖onCallStateChanged()
。多数民众赞成。
CallReceiver.Java
public class CallReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(new PhoneStateListener(){
@Override
public void onCallStateChanged(int state, String Number) {
super.onCallStateChanged(state, Number);
System.out.println("Number: " + Number);
// Compare your number to from this `Number` if it is found in you list then make ring state RINGER_MODE_NORMAL
// else make it to RINGER_MODE_SILENT
if (Number found in list) {
// to make Ring state silent
AudioManager audiomanager = (AudioManager)YourActivityName.this.getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else {
// to make Ring state Normal (Ringable)
AudioManager audiomanager = (AudioManager)YourActivityName.this.getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
},PhoneStateListener.LISTEN_CALL_STATE);
}
让戒指无声:
AudioManager audiomanager = (AudioManager)YourActivityName.this.getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
答案 1 :(得分:1)
清单:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!--This part is inside the application-->
<receiver android:name=".PhoneCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
创建新的java类并将其命名为PhoneCallReceiver
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.telephony.TelephonyManager;
import android.util.Log;
public abstract class PhoneCallReceiver extends BroadcastReceiver {
//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private static String savedNumber; //because the passed incoming is only valid in ringing
private String[] impNumber = new String[8];private String[] impNumber = new String[8]; // you can give 8 number for more replace 8 with any number you can alse use other method for contact save and read FileInputStream fin = openFileInput(file); to read and write value etc
@Override
public void onReceive(Context context, Intent intent) {
impNumber[0]="342342323";
impNumber[1]="234234234";
impNumber[7]="2342343544";
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;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
state = TelephonyManager.CALL_STATE_OFFHOOK;
} else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
state = TelephonyManager.CALL_STATE_RINGING;
}
onCallStateChanged(context, state, number);
}
//Derived classes should override these to respond to specific events of interest
protected void onIncomingCallReceived(Context ctx, String number, Date start){
if(impNumber[0].compareTo(number)!=0||impNumber[1].compareTo(number)!=0||impNumber[7].compareTo(number)!=0){
Log.e("Call Received" , "Number = "+ number);
AudioManager audiomanager = (AudioManager)ctx.getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);}
};
protected void onIncomingCallAnswered(Context ctx, String number, Date start){
Log.e("Call Answered" , "Number = "+ number);
};
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){
Log.e("Call Ended" , "Number = "+ number);
AudioManager audiomanager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
};
protected void onMissedCall(Context ctx, String number, Date start){
Log.e("Missed" , "Number = "+ number);
AudioManager audiomanager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
};
//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
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;
onIncomingCallReceived(context, number, 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 = true;
callStartTime = new Date();
onIncomingCallAnswered(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());
}
break;
}
lastState = state;
}
}
答案 2 :(得分:0)
您可以创建无声铃声并将其分配给某些联系人,而重要联系人将使用非静音铃声。