Skip to content

Commit 156cfaf

Browse files
author
wanghonghe
committed
双进程守护,解决安卓程序由于ndk崩溃不能重启的问题
0 parents  commit 156cfaf

File tree

79 files changed

+1194
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1194
-0
lines changed

.gitignore

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
### Android template
15+
# Built application files
16+
*.apk
17+
*.ap_
18+
*.aab
19+
20+
# Files for the ART/Dalvik VM
21+
*.dex
22+
23+
# Java class files
24+
*.class
25+
26+
# Generated files
27+
bin/
28+
gen/
29+
out/
30+
release/
31+
32+
# Gradle files
33+
.gradle/
34+
build/
35+
36+
# Local configuration file (sdk path, etc)
37+
local.properties
38+
39+
# Proguard folder generated by Eclipse
40+
proguard/
41+
42+
# Log Files
43+
*.log
44+
45+
# Android Studio Navigation editor temp files
46+
.navigation/
47+
48+
# Android Studio captures folder
49+
captures/
50+
51+
# IntelliJ
52+
*.iml
53+
.idea/workspace.xml
54+
.idea/tasks.xml
55+
.idea/gradle.xml
56+
.idea/assetWizardSettings.xml
57+
.idea/dictionaries
58+
.idea/libraries
59+
# Android Studio 3 in .gitignore file.
60+
.idea/caches
61+
.idea/modules.xml
62+
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
63+
.idea/navEditor.xml
64+
65+
# Keystore files
66+
# Uncomment the following lines if you do not want to check your keystore files in.
67+
#*.jks
68+
#*.keystore
69+
70+
# External native build folder generated in Android Studio 2.2 and later
71+
.externalNativeBuild
72+
73+
# Google Services (e.g. APIs or Firebase)
74+
# google-services.json
75+
76+
# Freeline
77+
freeline.py
78+
freeline/
79+
freeline_project_description.json
80+
81+
# fastlane
82+
fastlane/report.xml
83+
fastlane/Preview.html
84+
fastlane/screenshots
85+
fastlane/test_output
86+
fastlane/readme.md
87+
88+
# Version control
89+
vcs.xml
90+
91+
# lint
92+
lint/intermediates/
93+
lint/generated/
94+
lint/outputs/
95+
lint/tmp/
96+
# lint/reports/
97+

.idea/encodings.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/runConfigurations.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 28
5+
defaultConfig {
6+
applicationId "com.honghe.guardtest"
7+
minSdkVersion 15
8+
targetSdkVersion 28
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
sourceSets {
20+
main {
21+
jniLibs.srcDirs = ['src/main/libs']//默认为jniLibs
22+
}
23+
}
24+
25+
}
26+
27+
dependencies {
28+
implementation fileTree(dir: 'libs', include: ['*.jar'])
29+
implementation 'com.android.support:appcompat-v7:28.0.0'
30+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
31+
implementation 'androidx.annotation:annotation:1.0.2'
32+
testImplementation 'junit:junit:4.12'
33+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
34+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
35+
implementation 'org.jetbrains:annotations-java5:15.0'
36+
}

app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.honghe.guardtest;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.honghe.guardtest", appContext.getPackageName());
25+
}
26+
}

app/src/main/AndroidManifest.xml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.honghe.guardtest">
4+
5+
<application
6+
android:name=".MyApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:roundIcon="@mipmap/ic_launcher_round"
11+
android:supportsRtl="true"
12+
android:theme="@style/AppTheme">
13+
<activity android:name=".MainActivity">
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
21+
<service
22+
android:name=".LocalService"
23+
android:enabled="true"
24+
android:exported="true" />
25+
<service
26+
android:name=".RemoteService"
27+
android:enabled="true"
28+
android:exported="true"
29+
android:process=":RemoteProcess" />
30+
</application>
31+
32+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// IMyAidlInterface.aidl
2+
package com.honghe.guardtest;
3+
4+
// Declare any non-default types here with import statements
5+
6+
interface IMyAidlInterface {
7+
/**
8+
* Demonstrates some basic types that you can use as parameters
9+
* and return values in AIDL.
10+
*/
11+
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
12+
double aDouble, String aString);
13+
14+
String getServiceName();
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.honghe.guardtest;
2+
3+
public class JniLoader {
4+
static {
5+
System.loadLibrary("firstndk");
6+
}
7+
8+
public native String getHelloString();
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.honghe.guardtest;
2+
3+
import android.app.Service;
4+
import android.content.ComponentName;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.content.ServiceConnection;
8+
import android.os.IBinder;
9+
import android.os.RemoteException;
10+
import android.util.Log;
11+
import android.widget.Toast;
12+
13+
public class LocalService extends Service {
14+
private static final String TAG = LocalService.class.getName();
15+
private MyBinder mBinder;
16+
17+
private ServiceConnection connection = new ServiceConnection() {
18+
@Override
19+
public void onServiceConnected(ComponentName name, IBinder service) {
20+
IMyAidlInterface iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
21+
try {
22+
Log.e("LocalService", "connected with " + iMyAidlInterface.getServiceName());
23+
if (MyApplication.getMainActivity() == null) {
24+
Intent intent = new Intent(LocalService.this.getBaseContext(), MainActivity.class);
25+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
26+
getApplication().startActivity(intent);
27+
}
28+
} catch (RemoteException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
33+
@Override
34+
public void onServiceDisconnected(ComponentName name) {
35+
Toast.makeText(LocalService.this, "链接断开,重新启动 RemoteService", Toast.LENGTH_LONG).show();
36+
Log.e(TAG, "onServiceDisconnected: 链接断开,重新启动 RemoteService");
37+
startService(new Intent(LocalService.this, RemoteService.class));
38+
bindService(new Intent(LocalService.this, RemoteService.class), connection, Context.BIND_IMPORTANT);
39+
}
40+
};
41+
42+
public LocalService() {
43+
}
44+
45+
@Override
46+
public void onCreate() {
47+
super.onCreate();
48+
}
49+
50+
@Override
51+
public int onStartCommand(Intent intent, int flags, int startId) {
52+
Log.e(TAG, "onStartCommand: LocalService 启动");
53+
Toast.makeText(this, "LocalService 启动", Toast.LENGTH_LONG).show();
54+
startService(new Intent(LocalService.this, RemoteService.class));
55+
bindService(new Intent(LocalService.this, RemoteService.class), connection, Context.BIND_IMPORTANT);
56+
return START_STICKY;
57+
}
58+
59+
@androidx.annotation.Nullable
60+
@Override
61+
public IBinder onBind(Intent intent) {
62+
mBinder = new MyBinder();
63+
return mBinder;
64+
}
65+
66+
private class MyBinder extends IMyAidlInterface.Stub {
67+
68+
@Override
69+
public String getServiceName() throws RemoteException {
70+
return LocalService.class.getName();
71+
}
72+
73+
@Override
74+
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
75+
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)