diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f5efe32..014484e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,9 @@ + + + @@ -45,6 +48,13 @@ android:name="android.accessibilityservice" android:resource="@xml/accessibility_service_config" /> + + + = 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"); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/zfdang/touchhelper/MainActivity.java b/app/src/main/java/com/zfdang/touchhelper/MainActivity.java index 02027f6..2627c4f 100644 --- a/app/src/main/java/com/zfdang/touchhelper/MainActivity.java +++ b/app/src/main/java/com/zfdang/touchhelper/MainActivity.java @@ -1,5 +1,6 @@ package com.zfdang.touchhelper; +import android.content.Intent; import android.os.Bundle; import com.google.android.material.bottomnavigation.BottomNavigationView; @@ -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)); } } \ No newline at end of file