对于Android的PHONE_STATE广播接收器不工作 [英] Android broadcast receiver for PHONE_STATE is not working
本文介绍了对于Android的PHONE_STATE广播接收器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
朋友你好我是android的广播接收器工作,但它不工作。我对广播接收器code如下。
hello friends i am working on android broadcast receiver but it is not working. My code for broadcast receiver is given below.
public class MainActivity extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1){
Toast.makeText(arg0, "Service Explicitely", Toast.LENGTH_SHORT).show();
arg0.stopService(new Intent(arg0,CallRecordingService.class));
Intent intent=new Intent(arg0, CallRecordingService.class);
arg0.startService(intent);
Toast.makeText(arg0, "Service Explicitely", Toast.LENGTH_SHORT).show();
}
和manifiest文件也给出了
and the manifiest file is also given
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" ></action>
</intent-filter>
</receiver>
<service android:name=".CallRecordingService" />
</application>
请解决问题。
推荐答案
试试这个code。相信这将帮助你。其工作对我罚款。
Try this code. Sure it will help for you. Its working fine for me.
public class PhoneStatReceiver extends BroadcastReceiver{
private static final String TAG = "PhoneStatReceiver";
private static boolean incomingFlag = false;
private static String incoming_number = null;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
incomingFlag = false;
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i(TAG, "call OUT:"+phoneNumber);
}else{
TelephonyManager tm =
(TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
switch (tm.getCallState()) {
case TelephonyManager.CALL_STATE_RINGING:
incomingFlag = true;
incoming_number = intent.getStringExtra("incoming_number");
Log.i(TAG, "RINGING :"+ incoming_number);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(incomingFlag){
Intent callIntent = new Intent(context, RecordService.class);
context.startService(callIntent);
Log.i(TAG, "incoming ACCEPT :"+ incoming_number);
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if(incomingFlag){
context.stopService(new Intent(context, RecordService.class));
Log.i(TAG, "incoming IDLE");
}
break;
}
}
}
}
这篇关于对于Android的PHONE_STATE广播接收器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文