Skip to content

Commit 8a2950f

Browse files
author
SamsetDev
committed
App auto restart itself
App auto restart itself
1 parent 4f89e67 commit 8a2950f

32 files changed

+770
-0
lines changed

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 28
5+
defaultConfig {
6+
applicationId "com.app.restart"
7+
minSdkVersion 15
8+
targetSdkVersion 28
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
24+
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
25+
testImplementation 'junit:junit:4.12'
26+
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
27+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
28+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
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
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.app.restart;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.InstrumentationRegistry;
6+
import androidx.test.runner.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getTargetContext();
24+
25+
assertEquals("com.app.restart", appContext.getPackageName());
26+
}
27+
}

app/src/main/AndroidManifest.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.app.restart">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.app.restart;
2+
3+
import androidx.appcompat.app.AppCompatActivity;
4+
import androidx.appcompat.widget.AppCompatButton;
5+
6+
import android.annotation.SuppressLint;
7+
import android.app.AlarmManager;
8+
import android.app.PendingIntent;
9+
import android.content.Context;
10+
import android.content.Intent;
11+
import android.os.Build;
12+
import android.os.Bundle;
13+
import android.util.Log;
14+
import android.view.View;
15+
16+
public class MainActivity extends AppCompatActivity {
17+
private int REQUESTCODE = 1;
18+
private int RESTART_PERIOD = 1000 * 60 * 2; // set 2 min
19+
private AppCompatButton btnrestart;
20+
private String TAG=MainActivity.class.getSimpleName();
21+
22+
@Override
23+
protected void onCreate(Bundle savedInstanceState) {
24+
super.onCreate(savedInstanceState);
25+
setContentView(R.layout.activity_main);
26+
btnrestart=findViewById(R.id.btnrestart);
27+
btnrestart.setOnClickListener(new View.OnClickListener() {
28+
@Override
29+
public void onClick(View view) {
30+
restartApp(MainActivity.this);
31+
}
32+
});
33+
}
34+
35+
@SuppressLint("NewApi")
36+
private void restartApp(Context cnt) {
37+
38+
Intent intent = new Intent(cnt, MainActivity.class);
39+
40+
// PendingIntent
41+
PendingIntent pendingIntent = PendingIntent.getActivity(cnt, REQUESTCODE, intent,
42+
PendingIntent.FLAG_CANCEL_CURRENT);
43+
44+
// Setup AlarmManager
45+
AlarmManager alarmManager = (AlarmManager) cnt.getSystemService(Context.ALARM_SERVICE);
46+
47+
// set time 2 min
48+
if (alarmManager != null) {
49+
long trigger = System.currentTimeMillis() + RESTART_PERIOD;
50+
51+
if (Build.VERSION.SDK_INT >= 23) {
52+
Log.e(TAG, "alarm manager call if ");
53+
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC, trigger, pendingIntent);
54+
} else if (Build.VERSION.SDK_INT >= 19) {
55+
Log.e(TAG, "alarm manager call else if ");
56+
alarmManager.setExact(AlarmManager.RTC, trigger, pendingIntent);
57+
} else {
58+
Log.e(TAG, "alarm manager call else ");
59+
alarmManager.set(AlarmManager.RTC, trigger, pendingIntent);
60+
}
61+
62+
}
63+
64+
finish();
65+
}
66+
67+
@Override
68+
protected void onStart() {
69+
super.onStart();
70+
Log.e(TAG," call onStart() method ");
71+
}
72+
73+
@Override
74+
protected void onRestart() {
75+
super.onRestart();
76+
Log.e(TAG," call onRestart() method ");
77+
}
78+
79+
@Override
80+
protected void onResume() {
81+
super.onResume();
82+
Log.e(TAG," call onResume() method ");
83+
}
84+
85+
@Override
86+
protected void onPause() {
87+
super.onPause();
88+
Log.e(TAG," call onPause() method ");
89+
}
90+
91+
@Override
92+
protected void onStop() {
93+
super.onStop();
94+
Log.e(TAG," call onStop() method ");
95+
}
96+
97+
@Override
98+
protected void onDestroy() {
99+
super.onDestroy();
100+
Log.e(TAG," call onDestroy() method ");
101+
}
102+
103+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeWidth="1"
11+
android:strokeColor="#00000000">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeWidth="1"
33+
android:strokeColor="#00000000" />
34+
</vector>

0 commit comments

Comments
 (0)