即使打开或关闭应用程序,也不会收到通知。但是吐司只有在应用程序打开时才会出现。我有一个主要活动,另一个是扩展广播侦听器的类。在活动中,我正在请求权限,而在课堂上,我编写了用于通知来电的代码。请帮助我提供一些代码片段。下面是我的活动课。
public class PhoneActivity extends AppCompatActivity {
private static final int REQUEST_READ_PHONE_STATE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone);
int readContactsPermissionLog = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CALL_LOG);
if(readContactsPermissionLog != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, REQUEST_READ_PHONE_STATE);
}
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE_STATE);
}
}
}
这是我的扩展广播接收器的课程。
public class PhoneState extends BroadcastReceiver {
public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
private final static String default_notification_channel_id = "default" ;
boolean connected = true;
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state != null) {
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
if (!intent.getExtras().containsKey(TelephonyManager.EXTRA_INCOMING_NUMBER)) {
Log.i("Call receiver", "skipping intent=" + intent + ", extras=" + intent.getExtras() + " - no number was supplied");
return;
}
String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
System.out.println("incoming number is " + number);
if (number != null) {
if (number.equals("some number i have given for testing")) {
System.out.println("number matched");
showNotification(context);
// Toast.makeText(context, "number is " + number, Toast.LENGTH_SHORT).show();
} else {
showNotification(context);
// Toast.makeText(context, "number is " + number, Toast.LENGTH_LONG).show();
System.out.println("not matched");
}
} else {
System.out.println("number is null");
}
}
}
}
}
public void showNotification(Context context){
Intent testIntent = new Intent("com.example.gridlist.Activity.PhoneActivity");
PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0,testIntent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_menu_black_24dp)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setWhen(System.currentTimeMillis())
.setAutoCancel(false)
.setOngoing(true)
.setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_SOUND);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
我已经在清单文件中添加了它。
<receiver android:name=".PhoneState"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
这是我的布局活动。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity.PhoneActivity">
</LinearLayout>
请确保您的Android版本小于8,否则应使用通知频道创建通知https://developer.android.com/training/notify-user/channels