Skip to content

Commit

Permalink
🎉Release 1.3,调整背景运行代码提升安全性(BETA)、主函数适配背景运行更多功能(BETA)、调整背景运行机制(BETA)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hny0305Lin committed Jul 14, 2024
1 parent a7d72f5 commit 09ddedd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class MyForegroundService extends Service {
private Runnable stopSelfRunnable = new Runnable() {
@Override
public void run() {
stopSelf();
showCompletionNotification();
exitApp();
stopSelf(); //停止服务
showCompletionNotification(); //显示完成通知
exitApp(); //退出应用
}
};
private Runnable updateNotificationRunnable;
Expand All @@ -44,10 +44,10 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "服务已启动");

createNotificationChannel();
endTime = System.currentTimeMillis() + 10 * 60 * 1000; // 10分钟后停止服务
endTime = System.currentTimeMillis() + 10 * 60 * 1000; // 1分钟后停止服务
startForeground(1, createNotification("保持软件后台运行,已启用,请注意电池消耗,消耗过快请关闭本程序。"));

// 10分钟后自动停止服务
// 1分钟后自动停止服务
handler.postDelayed(stopSelfRunnable, 10 * 60 * 1000);

// 每秒更新通知
Expand All @@ -58,6 +58,9 @@ public void run() {
if (remainingTime > 0) {
startForeground(1, createNotification("剩余时间: " + remainingTime / 1000 + "秒"));
handler.postDelayed(this, 1000);
} else {
// 当计时结束时停止更新通知
handler.removeCallbacks(this);
}
}
};
Expand Down Expand Up @@ -121,10 +124,16 @@ private void exitApp() {
// 关闭所有Activity
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_EXIT_APP");
broadcastIntent.setPackage("com.haohanyh.linmengjia.nearlink.nlchat.fun");
sendBroadcast(broadcastIntent);

// 终止进程
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
}

public void stopActivities() {
Intent intent = new Intent("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_FINISH_ACTIVITY");
sendBroadcast(intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.cardview.widget.CardView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
Expand Down Expand Up @@ -172,11 +173,12 @@ public void run() {
stopService(new Intent(MainActivity.this, MyForegroundService.class));
}
};
private BroadcastReceiver exitReceiver = new BroadcastReceiver() {

private BroadcastReceiver finishActivityReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if ("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_EXIT_APP".equals(intent.getAction())) {
finish();
if ("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_FINISH_ACTIVITY".equals(intent.getAction())) {
ActivityCompat.finishAffinity(MainActivity.this);
}
}
};
Expand All @@ -185,6 +187,7 @@ public void onReceive(Context context, Intent intent) {
*
* @param savedInstanceState
*/
@SuppressLint({"ObsoleteSdkInt", "InlinedApi"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -196,13 +199,15 @@ protected void onCreate(Bundle savedInstanceState) {
return insets;
});

IntentFilter filter = new IntentFilter("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_EXIT_APP");
// 根据Android版本注册广播接收器,以确保应用的兼容性,注册监听退出应用的广播接收器
IntentFilter filter = new IntentFilter("com.haohanyh.linmengjia.nearlink.nlchat.fun.ACTION_FINISH_ACTIVITY");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(exitReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
registerReceiver(finishActivityReceiver, filter, Context.RECEIVER_NOT_EXPORTED);
} else {
registerReceiver(exitReceiver, filter);
registerReceiver(finishActivityReceiver, filter, Context.RECEIVER_EXPORTED);
}

// 设置屏幕常亮
if (MobileKeepScreenOn) {getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);}
Init();
}
Expand All @@ -223,6 +228,13 @@ protected void onStop() {
handler.postDelayed(stopServiceRunnable, 10 * 60 * 1000);
}

@Override
protected void onDestroy() {
super.onDestroy();
// 注销广播接收器
unregisterReceiver(finishActivityReceiver);
}

/**
* Init()为初始化软件控件、权限等内容,启动接下来的可使用的控件
*
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<resources>
<string name="app_name">NLChat</string>
<string name="app_package">com.haohanyh.linmengjia.nearlink.nlchat.fun</string>
<string name="app_version">1.3.249.2024.0713</string>
<string name="app_version">1.3.252.2024.0714</string>

<string name="appwarn">NLChat,浩瀚银河宗旨为用爱和魔法创造Android APP。</string>
<string name="thanks3q">友情感谢</string>
Expand Down

0 comments on commit 09ddedd

Please sign in to comment.