Android 广播接收机不工作
我正在尝试测试广播接收器是否有传出呼叫。请帮忙 这是我的CallCounter课程:Android 广播接收机不工作,android,broadcastreceiver,intentfilter,Android,Broadcastreceiver,Intentfilter,我正在尝试测试广播接收器是否有传出呼叫。请帮忙 这是我的CallCounter课程: package com.callout; import android.content.Context; import android.content.Intent; import android.telephony.PhoneStateListener; import android.telephony.TelephonyManager; import android.util.Log; public
package com.callout;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class CallCounter extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("Tony+++++++++++","Outgoing Call finished");
// Call Finished -> stop counter and store it.
// callStop=new Date().getTime();
Context c = getApplicationContext();
c.stopService(new Intent(c,ListenerContainer.class));
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("++++++++++++++++Tony","Outgoing Call Starting");
// Call Started -> start counter.
// This is not precise, because it starts when calling,
// we can correct it later reading from call log
// callStart=new Date().getTime();
break;
}
}
private Context getApplicationContext() {
// TODO Auto-generated method stub
return this.getApplicationContext();
}
}
这是我的接收器
package com.callout;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class myReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
Log.d("T+++++++++++++++++ony","In mYRecieverrr");
context.startService(new Intent(context,ListenerContainer.class));
}
}
}
这是我的服务:
package com.callout;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
public class ListenerContainer extends Service {
public class LocalBinder extends Binder {
ListenerContainer getService() {
Log.d("Tony","OKKK HEREEEEEEEEEEEEEeEEEEE");
return ListenerContainer.this;
}
}
@Override
public void onStart(Intent intent, int startId) {
TelephonyManager tManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
CallCounter callCounter=new CallCounter();
tManager.listen(callCounter,PhoneStateListener.LISTEN_CALL_STATE);
Log.d("To++++++++++++++ny","Call COUNTER Registered");
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private final IBinder mBinder = new LocalBinder();
}
如果我缺少一些权限,这就是清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.callout"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CallCounter"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".myReceiver"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".ListenerContainer" />
<receiver android:name=".myReceiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
</manifest>
请指出我的错误。您的收件人似乎在清单文件中缺少一个属性: 查看导出的属性,它应该设置为true以接收来自其他应用程序的广播 问候,,
Stéphane您的接收者似乎在清单文件中缺少一个属性: 查看导出的属性,它应该设置为true以接收来自其他应用程序的广播 问候,,
Stéphane您必须将以下权限添加到清单中,因为您正在注册phonestatelistener
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
您必须将以下权限添加到清单中,因为您正在注册phonestatelistener
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
这里有一个源代码完整的工作解决方案,可以满足您的需求
这里有一个源代码完整的工作解决方案,可以满足您的要求
你希望我们看看水晶球吗?您会遇到什么错误,LogCat显示了什么或什么不起作用?很抱歉,d不完整的问题,错误地删除了日志部分。接收器在f9工作,然后尝试启动服务。当我跟踪我的代码时,我发现它一直运行到onstart()方法,但之后不会启动phonestatelistener部分。这是一种错误:/AndroidRuntime(288):致命异常:main E/AndroidRuntime(288):java.lang.RuntimeException:无法启动服务com.callout。ListenerContainer@44ee2818意图为{cmp=com.callout/.ListenerContainer}:java.lang.SecurityException:用户10042和当前进程都没有Android.permission.READ\u PHONE\u状态您希望我们查看水晶球吗?您会遇到什么错误,LogCat显示了什么或什么不起作用?很抱歉,d不完整的问题,错误地删除了日志部分。接收器在f9工作,然后尝试启动服务。当我跟踪我的代码时,我发现它一直运行到onstart()方法,但之后不会启动phonestatelistener部分。这是一种错误:/AndroidRuntime(288):致命异常:main E/AndroidRuntime(288):java.lang.RuntimeException:无法启动服务com.callout。ListenerContainer@44ee2818意图为{cmp=com.callout/.ListenerContainer}:java.lang.SecurityException:用户10042和当前进程都没有Android.permission.READ\u手机_STATE@Stephane:谢谢你的回答,但我没有收到来自其他应用程序的广播,只是全球广播。在我的例子中,服务工作正常,但phonestatelistener没有实例化。请问你试过了吗?在我看来,全球广播并不意味着应用程序本地广播…@Stephane:谢谢你的回答,但我没有收到其他应用程序的广播,只是全球广播。在我的例子中,服务工作正常,但phonestatelistener没有实例化。请问你试过了吗?在我看来,全球广播并不意味着应用程序本地广播。。。