我有以下manifest.xml:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".PhoneStateBroadcastReceiver"
android:permission="android.permission.READ_PHONE_STATE">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
侦听器在主要活动运行时接收数据。但是,当主要活动被杀死时,接收器停止输出信息。
接收器是一个示例类:
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "PhoneReceiver";
Context mContext;
String incoming_number;
private int prev_state;
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); //TelephonyManager object
CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE); //Register our listener with TelephonyManager
Bundle bundle = intent.getExtras();
String phoneNr = bundle.getString("incoming_number");
Log.v(TAG, phoneNr);
Log.d( TAG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
mContext = context;
}
/* Custom PhoneStateListener */
public class CustomPhoneStateListener extends PhoneStateListener {
private static final String TAG = "CustomStateListener";
@Override
public void onCallStateChanged(int state, String incomingNumber){
if( incomingNumber != null && incomingNumber.length() > 0 )
incoming_number = incomingNumber;
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.d(TAG, "CALL_STATE_RINGING");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "CALL_STATE_OFFHOOK");
prev_state=state;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d(TAG, "CALL_STATE_IDLE==>"+incoming_number);
if((prev_state == TelephonyManager.CALL_STATE_OFFHOOK)){
prev_state=state;
//Answered Call which is ended
}
if((prev_state == TelephonyManager.CALL_STATE_RINGING)){
prev_state=state;
//Rejected or Missed call
}
break;
}
}
}
}
据我了解android原则,当接收器在清单中注册时,它应该接收数据,即使它的应用程序被杀死,如果是这样,我做错了什么?
答案 0 :(得分:1)
将以下内容添加到清单中的<receiver>
:
android:enabled="true"
android:exported="true">
此外according to this thread,您似乎必须在广播接收器开始工作之前手动启动您的一项活动,即应用程序必须在任何广播接收器工作之前至少启动一次。
另外在<application>
标记之外声明权限。所以你应该有这样的东西:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.app.myapp" >
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application ...
...
</manifest>
当然,这意味着您应该删除receiver
。
答案 1 :(得分:0)
行。在将我的头撞到墙上超过一周之后,我决定创建一个功能最少的新项目,并且从第一次尝试开始工作。我不知道它是否是由我的旧代码中的某些东西引起的,我怀疑,因为我将方法按方法复制到新项目中它仍然有效,或者更新版本的Android Studio及其构建工具。
非常感谢REG1有耐心和愿意提出想法。
此致
答案 2 :(得分:0)
将这些行添加到android studio清单文件中...... 你刚刚没有注册广播课
<receiver android:name=".PhoneStateBroadcastReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>