Browse Source

feat: update Android configuration and notification handling

- Add buildToolsVersion and enable multiDex in build.gradle
- Fix typo in RECEIVE_BOOT_COMPLETED permission
- Add new permissions for notifications and foreground service
- Set exported=false for activities and services
- Implement modern notification channel for API 26+
- Improve service lifecycle handling and notification management
liuyuqi-cnb 3 weeks ago
parent
commit
45bc9b3af8

+ 7 - 0
app/build.gradle

@@ -2,6 +2,7 @@ apply plugin: 'com.android.application'
 
 android {
     compileSdkVersion 33
+    buildToolsVersion '33.0.2'
     useLibrary 'org.apache.http.legacy'
 
     signingConfigs {
@@ -18,6 +19,7 @@ android {
         minSdkVersion 8
         targetSdkVersion 33
         signingConfig signingConfigs.debug
+        multiDexEnabled true
     }
 
     buildTypes {
@@ -28,6 +30,11 @@ android {
         }
     }
 
+    compileOptions {
+        sourceCompatibility JavaVersion.VERSION_1_8
+        targetCompatibility JavaVersion.VERSION_1_8
+    }
+
     dependencies {
         
     }

+ 14 - 7
app/src/main/AndroidManifest.xml

@@ -4,7 +4,7 @@
     package="me.yoqi.mobile.safe">
 
     <uses-permission android:name="android.permission.RECEIVE_SMS" />
-    <uses-permission android:name="android.permission.RECEIVE_BOOT_CPMPLETED" />
+    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.CALL_PHONE" />
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
@@ -13,6 +13,8 @@
     <uses-permission android:name="android.permission.READ_SMS" />
     <uses-permission android:name="android.permission.WRITE_SMS" />
     <uses-permission android:name="android.permission.SEND_SMS" />
+    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
+    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
 
 
     
@@ -22,7 +24,8 @@
 
     <application
         android:icon="@drawable/app_logo"
-        android:label="@string/app_name">
+        android:label="@string/app_name"
+        android:exported="false">
         <activity
             android:name=".MainActivity"
             android:configChanges="orientation|keyboardHidden"
@@ -36,16 +39,20 @@
         </activity>
         <activity
             android:name=".MessageActivity"
-            android:configChanges="orientation|keyboardHidden" />
+            android:configChanges="orientation|keyboardHidden"
+            android:exported="false" />
         <activity
             android:name=".CallActivity"
-            android:configChanges="orientation|keyboardHidden" />
+            android:configChanges="orientation|keyboardHidden"
+            android:exported="false" />
         <activity
             android:name=".SetActivity"
-            android:configChanges="orientation|keyboardHidden" />
+            android:configChanges="orientation|keyboardHidden"
+            android:exported="false" />
         <activity
             android:name=".SensitiveActivity"
-            android:configChanges="orientation|keyboardHidden" />
+            android:configChanges="orientation|keyboardHidden"
+            android:exported="false" />
 
         <receiver
             android:name="me.yoqi.mobile.tool.BroadCastTool"
@@ -61,7 +68,7 @@
 
         <service
             android:name="me.yoqi.mobile.tool.ServiceTool"
-            android:exported="true"
+            android:exported="false"
             android:permission="TODO">
             <intent-filter>
                 <action android:name="me.yoqi.mobile.safe.AUTO_START_SERVICE" />

+ 54 - 27
app/src/main/java/me/yoqi/mobile/tool/ServiceTool.java

@@ -1,7 +1,7 @@
 package me.yoqi.mobile.tool;
 
-import android.annotation.TargetApi;
 import android.app.Notification;
+import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.app.PendingIntent;
 import android.app.Service;
@@ -16,6 +16,8 @@ public class ServiceTool extends Service {
 
     private NotificationManager mNF;
     public static String AUTO_START = "com.ldci.android.t56.mobile.safe.AUTO_START";
+    private static final String CHANNEL_ID = "365_mobile_secretary_channel";
+    private static final int NOTIFICATION_ID = R.string.app_name;
 
     @Override
     public IBinder onBind(Intent arg0) {
@@ -25,51 +27,76 @@ public class ServiceTool extends Service {
     @Override
     public void onCreate() {
         super.onCreate();
+        createNotificationChannel();
     }
 
-    //	public static int serviceInt = 0;
     @Override
-    public void onStart(Intent intent, int startId) {
-        super.onStart(intent, startId);
-//		Toast.makeText(this, "开启服务,发出通知", Toast.LENGTH_LONG).show();
-//		new Thread(new Runnable(){
-//			public void run(){
-//				while(true){
-//					try {
-//						Thread.sleep(1000);
-//						serviceInt = 0;
-//					} catch (InterruptedException e) {
-//						e.printStackTrace();
-//					}
-//				}
-//			}
-//		}).start();
+    public int onStartCommand(Intent intent, int flags, int startId) {
         autoStartNotification();
+        return START_STICKY;
     }
 
     @Override
     public void onDestroy() {
         super.onDestroy();
-//		Toast.makeText(this, "停止服务,取消通知", Toast.LENGTH_LONG).show();
-        mNF.cancel(R.string.app_name);
+        if (mNF != null) {
+            mNF.cancel(NOTIFICATION_ID);
+        }
+    }
+
+    private void createNotificationChannel() {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            CharSequence name = getString(R.string.app_name);
+            String description = "365手机秘书服务通知";
+            int importance = NotificationManager.IMPORTANCE_LOW;
+            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
+            channel.setDescription(description);
+            
+            if (mNF == null) {
+                mNF = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
+            }
+            mNF.createNotificationChannel(channel);
+        }
     }
 
-    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
     private void autoStartNotification() {
-        Notification mNotification = new Notification(R.drawable.app_logo, "365手机秘书", System.currentTimeMillis());
         Intent intent = new Intent(this, MainActivity.class);
         intent.setAction(AUTO_START);
         intent.putExtra("auto_start", "boot_completed");
-        PendingIntent mPI = PendingIntent.getActivity(this, 0, intent, 0);
+        
+        int pendingIntentFlags;
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+            pendingIntentFlags = PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT;
+        } else {
+            pendingIntentFlags = PendingIntent.FLAG_UPDATE_CURRENT;
+        }
+        
+        PendingIntent mPI = PendingIntent.getActivity(this, 0, intent, pendingIntentFlags);
 
-//		mNotification.setLatestEventInfo(this, "365手机秘书", "成功开机启动", mPI);
-//        Notification.Builder builder = new Notification.Builder(MainActivity.this);
-//        builder.setAutoCancel(true);
+        Notification notification;
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            notification = new Notification.Builder(this, CHANNEL_ID)
+                    .setContentTitle("365手机秘书")
+                    .setContentText("成功开机启动")
+                    .setSmallIcon(R.drawable.app_logo)
+                    .setContentIntent(mPI)
+                    .setAutoCancel(true)
+                    .build();
+        } else {
+            notification = new Notification.Builder(this)
+                    .setContentTitle("365手机秘书")
+                    .setContentText("成功开机启动")
+                    .setSmallIcon(R.drawable.app_logo)
+                    .setContentIntent(mPI)
+                    .setAutoCancel(true)
+                    .setWhen(System.currentTimeMillis())
+                    .getNotification();
+        }
 
-        if (null == mNF) {
+        if (mNF == null) {
             mNF = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         }
-        mNF.notify(R.string.app_name, mNotification);
+        mNF.notify(NOTIFICATION_ID, notification);
     }
 
 }