Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

后台常驻问题 #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>


<queries>
<intent>
Expand Down Expand Up @@ -45,6 +48,13 @@
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
<!-- ["connectedDevice" | "dataSync" | "location" | "mediaPlayback" | "mediaProjection" | "phoneCall"]-->
<service
android:name=".ForeGroundService"
android:foregroundServiceType="dataSync"
android:enabled="true"
>
</service>

<receiver
android:name=".TouchHelperServiceReceiver"
Expand Down
73 changes: 73 additions & 0 deletions app/src/main/java/com/zfdang/touchhelper/ForeGroundService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.zfdang.touchhelper;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

public class ForeGroundService extends Service {
private static final String TAG = ForeGroundService.class.getSimpleName();
NotificationManager notificationManager;
String notificationId = "touch";
String notificationName = "常驻服务";

private void startForegroundService() {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//创建 NotificationChannel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(notificationId, notificationName, NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
startForeground(1, getNotification());

}

private Notification getNotification() {
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_touch_helper_icon)
.setContentTitle(getString(R.string.app_name))
.setContentText(getString(R.string.app_name) + "正在运行...");

//设置Notification的ChannelID,否则不能正常显示
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(notificationId);
}
Notification notification = builder.build();
return notification;

}

@Override
public void onCreate() {
super.onCreate();
startForegroundService();
Log.d(TAG, "onCreate()");
}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand()");
return super.onStartCommand(intent, flags, startId);
}


@Override
public void onDestroy() {
Log.d(TAG, "onDestroy()");
stopForeground(true);// 停止前台服务--参数:表示是否移除之前的通知
super.onDestroy();
}

@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind()");
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/com/zfdang/touchhelper/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zfdang.touchhelper;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.bottomnavigation.BottomNavigationView;
Expand All @@ -26,6 +27,8 @@ protected void onCreate(Bundle savedInstanceState) {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
// Log.d(MainActivity.class.getSimpleName(), "onStartCommand()");
startForegroundService(new Intent(getApplicationContext(), ForeGroundService.class));
}

}