来电的 BroadcastReceiver
BroadcastReceiver for incoming calls
在我的应用程序中,我希望在有来电时收到通知。为此,我创建了一个如下所示的 BroadcastReceiver:
public void onReceive(Context context, Intent intent)
{
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
if(state == TelephonyManager.CALL_STATE_RINGING) incommingCall(incomingNumber);
else if(state == TelephonyManager.CALL_STATE_IDLE) endingCall(incomingNumber);
}
//...
}
我现在的问题是从未调用过此 Receiver。我已经像这样在清单中注册了它:
<receiver
android:name="com.cilenco.interrupts.ContactControl"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
这有什么问题吗?或者这是我无法从清单中注册的接收器?如果是这样,您是否有其他想法如何在用户不必启动我的应用程序的情况下实现这一点?
Try this one:
public class Incomingcallreceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
Do your stuff
}
}
将此添加到清单中以注册呼叫的接收者
<receiver
android:name="com.example.enwaye_connect.Incomingcallreceiver"
android:label="Incomingcallreceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
同时添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_LOGS" />
让你android:exported="false"
属性为真
android:exported="true"
以便 android OS 可以将广播发送到您的应用程序,如果您指定导出为 false android OS 将忽略您的应用程序。
在我的应用程序中,我希望在有来电时收到通知。为此,我创建了一个如下所示的 BroadcastReceiver:
public void onReceive(Context context, Intent intent)
{
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class MyPhoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
if(state == TelephonyManager.CALL_STATE_RINGING) incommingCall(incomingNumber);
else if(state == TelephonyManager.CALL_STATE_IDLE) endingCall(incomingNumber);
}
//...
}
我现在的问题是从未调用过此 Receiver。我已经像这样在清单中注册了它:
<receiver
android:name="com.cilenco.interrupts.ContactControl"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
这有什么问题吗?或者这是我无法从清单中注册的接收器?如果是这样,您是否有其他想法如何在用户不必启动我的应用程序的情况下实现这一点?
Try this one:
public class Incomingcallreceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
Do your stuff
}
}
将此添加到清单中以注册呼叫的接收者
<receiver
android:name="com.example.enwaye_connect.Incomingcallreceiver"
android:label="Incomingcallreceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
同时添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.READ_LOGS" />
让你android:exported="false"
属性为真
android:exported="true"
以便 android OS 可以将广播发送到您的应用程序,如果您指定导出为 false android OS 将忽略您的应用程序。