我怎么知道 BroadcastReceiver 是否正常工作?
How do I know if BroadcastReceiver is working?
我正在尝试做一个应用程序,可以将来自来电的 phone 号码(以及联系人姓名,仅当它已经注册时)发送到网络服务,所以我找到了这个 question 在这里,我按照以下方式实现了答案:
public class PhoneStateReceiver extends BroadcastReceiver {
public String phoneNumber;
public String contactName;
ShareUuid su = new ShareUuid();
String uuid;
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.getString("state").equals(TelephonyManager.EXTRA_STATE_RINGING)) {
phoneNumber = extras.getString("incoming_number");
Log.e("PhoneNumber", "onReceive: "+phoneNumber);
String nombre_prueba = contactExists(context, phoneNumber);
Log.e("nombre_prueba", nombre_prueba);
uuid = su.getUuid();
Log.e("UUID - PhoneStateReceiver", uuid);
Uri uri = Uri.withAppendedPath(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
Cursor rCursor = context.getContentResolver().query(uri,
new String[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID},
null,null,null);
String nombre = rCursor.getString(0);
//new SendData(uuid, phoneNumber, nombre).execute();
Log.e("rCursor", nombre);
}
}
}
public String contactExists(Context context, String number) {
/// number is the phone number
Uri lookupUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
};
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
Log.d("Phone number ID", ContactsContract.CommonDataKinds.Phone._ID);
Log.d("Phone number DISPLAY_NAME", ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
contactName = ContactsContract.CommonDataKinds.Phone._ID;
return contactName;
}
} finally {
if (cur != null) cur.close();
contactName = "Número no registrado";
}
return contactName;
}
}
}
并且没有编译错误,但是当我接到一个电话时,logcat 或“运行”部分没有显示任何内容,所以,我不知道它是否已经开始工作了,这是我来自 Android Manifest:
的 intent-filter
<receiver
android:name=".PhoneStateReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
我认为这可能是我的错误,所以我按照此 tutotial 接收短信,但当我收到短信时它也没有显示任何内容。
我错过了什么?
我只是忘了在运行时请求权限,我在我的 onCreate 中添加了它并且完美运行:
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.READ_PHONE_STATE
)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{(Manifest.permission.READ_PHONE_STATE)},
REQUEST_PERMISSION_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.READ_CONTACTS
)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{(Manifest.permission.READ_CONTACTS)},
REQUEST_PERMISSION_READ_CONTACTS);
}
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.READ_CALL_LOG
)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{(Manifest.permission.READ_CALL_LOG)},
REQUEST_PERMISSION_READ_CALL_LOG);
}
我还修改了我的 PhoneStateReceiver class,当来电号码已经注册时,我在尝试获取联系人姓名时遇到了很多问题,我在 Whosebug 上发现了一些问题,但不幸的是 none他们中的一些人帮助了我(他们被弃用了解决方案),最后我设法让它与下面的代码一起工作,我希望它对其他人有帮助
public class PhoneStateReceiver extends BroadcastReceiver {
public String phoneNumber = "";
public String contactName = "";
@Override
public void onReceive(Context context, Intent intent) {
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING) && phoneNumber!=null){
Toast.makeText(context,"Ringing State Number is - " + phoneNumber, Toast.LENGTH_SHORT).show();
contactName = getContactName(context, phoneNumber);
}
}
catch (Exception e){
e.printStackTrace();
}
}
public String getContactName(Context context, String phoneNumber) {
if(phoneNumber == null){
return null;
}
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if(cursor == null) {
return null;
}
String contactName = "Contacto no registrado";
if(cursor.moveToFirst()) {
contactName = cursor.getString(0);
}
if(!cursor.isClosed()) {
cursor.close();
}
return contactName;
}
}
我正在尝试做一个应用程序,可以将来自来电的 phone 号码(以及联系人姓名,仅当它已经注册时)发送到网络服务,所以我找到了这个 question 在这里,我按照以下方式实现了答案:
public class PhoneStateReceiver extends BroadcastReceiver {
public String phoneNumber;
public String contactName;
ShareUuid su = new ShareUuid();
String uuid;
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving an Intent broadcast.
//throw new UnsupportedOperationException("Not yet implemented");
Bundle extras = intent.getExtras();
if (extras != null) {
if (extras.getString("state").equals(TelephonyManager.EXTRA_STATE_RINGING)) {
phoneNumber = extras.getString("incoming_number");
Log.e("PhoneNumber", "onReceive: "+phoneNumber);
String nombre_prueba = contactExists(context, phoneNumber);
Log.e("nombre_prueba", nombre_prueba);
uuid = su.getUuid();
Log.e("UUID - PhoneStateReceiver", uuid);
Uri uri = Uri.withAppendedPath(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
Cursor rCursor = context.getContentResolver().query(uri,
new String[] {
ContactsContract.CommonDataKinds.Phone.CONTACT_ID},
null,null,null);
String nombre = rCursor.getString(0);
//new SendData(uuid, phoneNumber, nombre).execute();
Log.e("rCursor", nombre);
}
}
}
public String contactExists(Context context, String number) {
/// number is the phone number
Uri lookupUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI,
Uri.encode(number));
String[] mPhoneNumberProjection = {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
};
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
if (cur.moveToFirst()) {
Log.d("Phone number ID", ContactsContract.CommonDataKinds.Phone._ID);
Log.d("Phone number DISPLAY_NAME", ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
contactName = ContactsContract.CommonDataKinds.Phone._ID;
return contactName;
}
} finally {
if (cur != null) cur.close();
contactName = "Número no registrado";
}
return contactName;
}
}
}
并且没有编译错误,但是当我接到一个电话时,logcat 或“运行”部分没有显示任何内容,所以,我不知道它是否已经开始工作了,这是我来自 Android Manifest:
的 intent-filter<receiver
android:name=".PhoneStateReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
我认为这可能是我的错误,所以我按照此 tutotial 接收短信,但当我收到短信时它也没有显示任何内容。
我错过了什么?
我只是忘了在运行时请求权限,我在我的 onCreate 中添加了它并且完美运行:
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.READ_PHONE_STATE
)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{(Manifest.permission.READ_PHONE_STATE)},
REQUEST_PERMISSION_PHONE_STATE);
}
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.READ_CONTACTS
)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{(Manifest.permission.READ_CONTACTS)},
REQUEST_PERMISSION_READ_CONTACTS);
}
if (ContextCompat.checkSelfPermission(
this, Manifest.permission.READ_CALL_LOG
)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{(Manifest.permission.READ_CALL_LOG)},
REQUEST_PERMISSION_READ_CALL_LOG);
}
我还修改了我的 PhoneStateReceiver class,当来电号码已经注册时,我在尝试获取联系人姓名时遇到了很多问题,我在 Whosebug 上发现了一些问题,但不幸的是 none他们中的一些人帮助了我(他们被弃用了解决方案),最后我设法让它与下面的代码一起工作,我希望它对其他人有帮助
public class PhoneStateReceiver extends BroadcastReceiver {
public String phoneNumber = "";
public String contactName = "";
@Override
public void onReceive(Context context, Intent intent) {
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING) && phoneNumber!=null){
Toast.makeText(context,"Ringing State Number is - " + phoneNumber, Toast.LENGTH_SHORT).show();
contactName = getContactName(context, phoneNumber);
}
}
catch (Exception e){
e.printStackTrace();
}
}
public String getContactName(Context context, String phoneNumber) {
if(phoneNumber == null){
return null;
}
ContentResolver cr = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = cr.query(uri, new String[] {ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
if(cursor == null) {
return null;
}
String contactName = "Contacto no registrado";
if(cursor.moveToFirst()) {
contactName = cursor.getString(0);
}
if(!cursor.isClosed()) {
cursor.close();
}
return contactName;
}
}