我有一个服务,我需要触发一个简单的通知,而不需要启动器活动,所以基本上是一个没有活动的应用程序,但由于某种原因,它不起作用,因为我也尝试从终端运行服务,但它不起作用,有什么可以做的吗?完成这个工作,谢谢
public class NetworkService extends android.app.Service {
private NotificationManager notificationManager;
@Override
public void onCreate() {
super.onCreate();
showNetworkChangeNotification();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void showNetworkChangeNotification() {
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("network_change_channel", "Network Change Channel", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.custom_ui_layout);
remoteViews.setTextViewText(R.id.text,"This is a testing text");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "network_change_channel")
.setContentTitle("Network ")
.setContentText("Sample")
.setSmallIcon(R.drawable.baseline_circle_notifications_24)
.setContentIntent(pendingIntent)
.setContent(remoteViews)
.addAction(R.drawable.baseline_circle_notifications_24, "Reset", getCancelPendingIntent())
.setAutoCancel(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(1, notificationBuilder.build());
} else {
notificationManager.notify(1, notificationBuilder.build());
}
}
private PendingIntent getCancelPendingIntent() {
Intent intent = new Intent(this, NetworkChangeReceiver.class);
intent.setAction("com.z.widgets.networkChangeReceiver.actionCancelNotification");
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
}
@Override
public void onDestroy() {
super.onDestroy();
notificationManager.cancel(1);
stopSelf();
}
}
public class NetworkChangeReceiver extends BroadcastReceiver {
private TelephonyManager telephonyManager;
private NotificationManager notificationManager;
@SuppressLint("MissingPermission")
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, NetworkService.class);
if (intent != null && "com.z.widgets.networkChangeReceiver.actionCancelNotification".equals(intent.getAction())) {
Toast.makeText(context,"Reset Clicked",Toast.LENGTH_LONG).show();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:name=".BaseApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WidgetJava"
tools:targetApi="31">
<service android:name=".NetworkService"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="com.example.action.START_FOREGROUND_SERVICE" />
</intent-filter>
</service>
<receiver android:name=".NetworkChangeReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.z.widgets.networkChangeReceiver.actionCancelNotification"/>
</intent-filter>
</receiver>
</application>
</manifest>
你有几个问题。
首先,虽然您拥有
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
,但这是运行时权限。在 Android 13 及更高版本上,您需要在运行时请求它。反过来,这将需要一项活动。
第二,你有:
<service android:name=".NetworkService"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="com.example.action.START_FOREGROUND_SERVICE" />
</intent-filter>
</service>
您似乎从
<intent-filter>
元素复制了 <activity>
的一部分。 <action android:name="android.intent.action.MAIN" />
与 <category android:name="android.intent.category.LAUNCHER" />
一起用于活动,特别是那些应该出现在启动器中的活动。如果您期望这些线路使您的服务运行,您会失望的。
第三,您的服务有:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);
这表示您没有进行某项活动。
第四,你有:
<receiver android:name=".NetworkChangeReceiver"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="com.z.widgets.networkChangeReceiver.actionCancelNotification"/>
</intent-filter>
</receiver>
使用隐式
Intent
(例如仅使用动作字符串)的广播多年来已基本上被禁止。目前尚不清楚什么代码将使用显式 Intent
来触发您的接收器。
我需要触发一个简单的通知,而不需要启动器活动,所以基本上是一个没有活动的应用程序
除非你是:
...那么你的代码将永远不会运行。在前两种情况下,您需要在其他地方编写代码或配置来安排代码运行。