即使在启动后也能收听 phone 状态
Listen to phone state even after boot
在我的应用程序中,我使用 Broadcast Receiver
收听 phone 状态事件(即收到呼叫、空闲等)。代码如下:
public class PhoneCallreceiver extends BroadcastReceiver {
private boolean ringing;
private boolean received;
public PhoneCallreceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.v("ranjith", "enterted receiver");
Bundle bundle = intent.getExtras();
String incomingnumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.v("ranjapp", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
ringing = true;
received = false;
}
}
Manifest.xml
<receiver
android:name=".PhoneCallreceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
我的问题是我想在设备重新启动后自动启动 BroadCast Receiver
。我有一个 BroadCast Receiver
来收听启动,但我不确定如何启动 PhoneCallreceiver
BroadCast 接收器。
Manifest.xml
<receiver
android:name=".backend.BootStarter"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
代码:
public class BootStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PhoneCallreceiver callreceiver = new PhoneCallreceiver();
context.registerReceiver(callreceiver,new IntentFilter(**what to put here**));
}
}
找到我的问题的答案,接收 phonestate 的 intentfilter 操作是 android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED
我的更新代码如下:
public class BootStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PhoneCallreceiver callreceiver = new PhoneCallreceiver();
context.registerReceiver(new PhoneCallreceiver(), new IntentFilter(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED));
}
}
在我的应用程序中,我使用 Broadcast Receiver
收听 phone 状态事件(即收到呼叫、空闲等)。代码如下:
public class PhoneCallreceiver extends BroadcastReceiver {
private boolean ringing;
private boolean received;
public PhoneCallreceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.v("ranjith", "enterted receiver");
Bundle bundle = intent.getExtras();
String incomingnumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.v("ranjapp", state);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
ringing = true;
received = false;
}
}
Manifest.xml
<receiver
android:name=".PhoneCallreceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
我的问题是我想在设备重新启动后自动启动 BroadCast Receiver
。我有一个 BroadCast Receiver
来收听启动,但我不确定如何启动 PhoneCallreceiver
BroadCast 接收器。
Manifest.xml
<receiver
android:name=".backend.BootStarter"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
代码:
public class BootStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PhoneCallreceiver callreceiver = new PhoneCallreceiver();
context.registerReceiver(callreceiver,new IntentFilter(**what to put here**));
}
}
找到我的问题的答案,接收 phonestate 的 intentfilter 操作是 android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED
我的更新代码如下:
public class BootStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PhoneCallreceiver callreceiver = new PhoneCallreceiver();
context.registerReceiver(new PhoneCallreceiver(), new IntentFilter(android.telephony.TelephonyManager.ACTION_PHONE_STATE_CHANGED));
}
}