我想自定义电话响铃视图并显示并显示textView。现在如何访问该视图并对其进行自定义?或者如果我无法访问它,可以在该视图中显示TextView怎么办?
最佳答案
您需要创建一个应用程序,并在活动中扩展BroadcastListener服务,并为该活动提供所需的布局
1)在AndroidManifest.xml中声明广播接收器
<receiver android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
也是这个许可
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
您的主要MainActivity.java应该类似于此文件
public class MainActivity extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
try {
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch (Exception e) {
Log.e("Phone Receive Error", " " + e);
}
}
private class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.d("MyPhoneListener",state+" incoming no:"+incomingNumber);
if (state == 1) {
String msg = "New Phone Call Event. Incomming Number : "+incomingNumber;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(pcontext, msg, duration);
toast.show();
}
}
}
}
接到电话后,此代码将显示一个祝酒词,您可以更改布局文件并根据需要显示。
关于java - 手机响铃时如何查看,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25862705/