call.java:
public class Call extends Activity{
boolean timerhasstarted;
Intent callIntent;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
call();
}
void call()
{
String num="7829893070";
callIntent=new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+num));
if(!timerhasstarted)
{
startActivity(callIntent);
ct.start();
timerhasstarted=true;
}
else {
ct.cancel();
timerhasstarted=false;
Toast.makeText(getApplicationContext(), "timer not started ",Toast.LENGTH_SHORT ).show();
}
}
CountDownTimer ct=new CountDownTimer(10000,1000) {
@Override
public void onTick(long millisUntilFinished) {
Toast.makeText(getApplicationContext(), "time: "+millisUntilFinished/1000, Toast.LENGTH_SHORT).show();
}
@Override
public void onFinish() {
Toast.makeText(getApplicationContext(), "time over ..",Toast.LENGTH_SHORT ).show();
OutgoingCallReceiver out=new OutgoingCallReceiver();
out.onReceive(getApplicationContext(),callIntent);
}
};
}
OutgoingCallReceiver.java:
public class OutgoingCallReceiver extends BroadcastReceiver {
public static final String ABORT_PHONE_NUMBER = "7204230210";
private static final String OUTGOING_CALL_ACTION = "android.intent.action.NEW_OUTGOING_CALL";
private static final String INTENT_PHONE_NUMBER = "android.intent.extra.PHONE_NUMBER";
String TAG="EMERGENCY";
@Override
public void onReceive(final Context context, final Intent intent) {
Log.v(TAG, "OutgoingCallReceiver .. : onReceive");
Log.i( "l", "onReceive()" );
Log.i( "l", "context: " + context );
Log.i( "l", "intent: " + intent );
String getphoneNumber = this.getResultData();
Log.i(TAG,"getphnum "+getphoneNumber);
String phoneNumber1 = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG,"PHONE_NUMBER "+phoneNumber1);
Toast.makeText(context, "PHONE_NUMBER "+phoneNumber1, Toast.LENGTH_LONG).show();
if (intent.getAction().equals(OutgoingCallReceiver.OUTGOING_CALL_ACTION)) {
Log.v(TAG, "OutgoingCallReceiver NEW_OUTGOING_CALL received");
Toast.makeText(context, "OutgoingCallReceiver NEW_OUTGOING_CALL received", Toast.LENGTH_SHORT).show();
// get phone number from bundle
String phoneNumber = intent.getExtras().getString("android.intent.action.NEW_OUTGOING_CALL");
if ((phoneNumber != null) && phoneNumber.equals(OutgoingCallReceiver.ABORT_PHONE_NUMBER)) {
Toast.makeText(context, "NEW_OUTGOING_CALL intercepted to number 123-123-1234 - aborting call",
Toast.LENGTH_LONG).show();
abortBroadcast();
// this.setResultData(ABORT_PHONE_NUMBER);
}
}
}
电话号码
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); //获得空号
这里写的是哪个语句都是为了获取phonenumber?
想要获取拨出的电话号码,但是在我检查的logcat和我的手机中,它显示为空值。为什么呢?
setResultData(空)
logcat的
03-15 11:50:06.062: V/EMERGENCY(490): OutgoingCallReceiver .. : onReceive
03-15 11:50:06.082: I/l(490): onReceive()
03-15 11:50:06.082: I/l(490): context: android.app.Application@44f3f8b0
03-15 11:50:06.082: I/l(490): intent: Intent { act=android.intent.action.CALL dat=tel:7829893070 }
03-15 11:50:06.113: I/EMERGENCY(490): getphnum null
03-15 11:50:06.122: I/EMERGENCY(490): PHONE_NUMBER null
03-15 11:50:10.522: D/dalvikvm(264): GC_EXPLICIT freed 71 objects / 3424 bytes in 189ms
03-15 11:50:15.653: D/dalvikvm(166): GC_EXPLICIT freed 4298 objects / 244840 bytes in 218ms
这里是getphnum& PHONE_NUMBER显示为null。通过toast,我也知道手机设备中的null。
答案 0 :(得分:1)
您可以尝试使用此活动,然后执行此操作后台服务
public void outgoingRecord()
{
Cursor c = getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI,
null,
null,
null,
android.provider.CallLog.Calls.DATE+ " DESC");
startManagingCursor(c);
int numberColumn = c.getColumnIndex(
android.provider.CallLog.Calls.NUMBER);
int dateColumn = c.getColumnIndex(
android.provider.CallLog.Calls.DATE);
// type can be: Incoming, Outgoing or Missed
int typeColumn = c.getColumnIndex(
android.provider.CallLog.Calls.TYPE);
int durationColumn=c.getColumnIndex(
android.provider.CallLog.Calls.DURATION);
// Will hold the calls, available to the cursor
ArrayList<String> callList = new ArrayList<String>();
try{
boolean moveToFirst=c.moveToFirst();
Log.e("MOVETOFIRST", "moveToFirst="+moveToFirst);
}
catch(Exception e)
{
Log.e("MOVETOFIRSTERROR","MOVETOFIRST Error="+e.toString());
}
String callerPhoneNumber = c.getString(numberColumn);
int callDate = c.getInt(dateColumn);
int callType = c.getInt(typeColumn);
int duration=c.getInt(durationColumn);
Log.d("CALLS", "callDate="+callDate);
switch(callType){
case android.provider.CallLog.Calls.INCOMING_TYPE:
Log.d("INCOMINGCALLLOG", "CallerPhoneNum="+
callerPhoneNumber+" "+"Duration="+duration);
break;
case android.provider.CallLog.Calls.MISSED_TYPE:
break;
case android.provider.CallLog.Calls.OUTGOING_TYPE:
Log.d("OUTGOINGCALLLOG",
"CallerPhoneNum="+ callerPhoneNumber+" "+"Duration="+duration);
break;
}
}