Application class recreated each time the screen goes on - android

The steps that explains the problem I faced are below;
1. I start the android application.
2. I put it on backgorund by pressing home button.
3. I turn the screen off.
4. I turn the screen on after some seconds.
5. My application suddenly becomes foreground. I realised that the application is forced to start again and the application class that I used is created again and then the start page of my application (this is login page) is shown although what page is opened before the step 2.
What I have tried so far is below;
I put configChanges and screenOrientation in all the activiy tags in my Androidmanifest.xml (I tried to put them one by one and both together also.)
<activity android:configChanges="orientation|keyboardHidden|screenSize"
android:name="com.myproject.Activity"
android:screenOrientation="nosensor"/>
I also tried to use screenOrientation="portrait" but didn't work.
My problem is related with the How do I disable orientation change on Android?, Prevent Android activity from being recreated on turning screen off and like others, but I could not find the solution of my problem yet.
Can you please help?
Edit 1: ********************************
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myproject"
android:versionCode="1"
android:versionName="1.0"
xmlns:tools="http://schemas.android.com/tools" >
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- Permission to use NFC -->
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc.hce"
android:required="true" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<permission android:name="com.myproject.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.myproject.permission.C2D_MESSAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<!--
Add this permission to check which network access properties (e.g.
active type: 3G/WiFi).
-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Add this permission to access WLAN MAC address. -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- Add this permission to access HW ID. -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<application
android:name="com.myproject.hce.MyApplication"
android:hardwareAccelerated="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:largeHeap="true"
android:theme="#style/MyAppTheme"
tools:replace="android:icon,android:theme"
>
<!-- Splash Activity -->
<activity android:configChanges="orientation|keyboardHidden|screenSize"
android:name=".Splash"
android:theme="#android:style/Theme.Holo.Light.NoActionBar"
android:windowSoftInputMode="stateHidden"
android:screenOrientation="nosensor" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity android:configChanges="orientation|keyboardHidden|screenSize"
android:name="com.myproject.LoginView"
android:screenOrientation="nosensor"
android:windowSoftInputMode="adjustResize" >
</activity>
<activity android:configChanges="orientation|keyboardHidden|screenSize"
android:name="com.myproject.MainView"
android:screenOrientation="nosensor" >
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/fb_app_id" />
<meta-data
android:name="com.crashlytics.ApiKey"
android:value="12h3g21h4v32hv43hv4" />
<service
android:name="com.myproject.pushnotifications.GCMIntentService"
android:enabled="true" >
</service>
<service
android:name="com.myproject.hce.McbpHceService"
android:exported="true"
android:permission="android.permission.BIND_NFC_SERVICE" >
<intent-filter>
<action android:name="android.nfc.cardemulation.action.HOST_APDU_SERVICE" />
</intent-filter>
<meta-data
android:name="android.nfc.cardemulation.host_apdu_service"
android:resource="#xml/apduservice" />
</service>
<receiver
android:name="com.myproject.pushnotifications.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RETRY" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
<service
android:name="com.myproject.hce.MyService"
android:exported="true"
android:enabled="true"
android:stopWithTask="false"/>
</application>
</manifest>
Application class
public class MyApplication extends Application {
private static MyApplication MyApplication;
private MyActivityLifeCycleCallbacks MyActivityLifeCycleCallbacks;
public static MyApplication getMyApplication() {
return MyApplication;
}
#Override
public void onCreate() { //coko1
MyLog.i(UtilConstants.LOG_TAG_HCE, "MyApplication " + "onCreate ...");
if (MyApplication == null)
MyApplication = this;
else
return;
MyLog.i(UtilConstants.LOG_TAG_HCE, "MyApplication " + "super.onCreate() ...");
super.onCreate();
registerActivityLifecycleCallbacks(new MyLifecycleHandler());
setMyActivityLifeCycleCallbacks(new MyActivityLifeCycleCallbacks());
registerActivityLifecycleCallbacks(getMyActivityLifeCycleCallbacks());
}
public MyActivityLifeCycleCallbacks getMyActivityLifeCycleCallbacks() {
return MyActivityLifeCycleCallbacks;
}
public void setMyActivityLifeCycleCallbacks(
MyActivityLifeCycleCallbacks MyActivityLifeCycleCallbacks) {
this.MyActivityLifeCycleCallbacks = MyActivityLifeCycleCallbacks;
}
}
Edit 2:****************
Splash.java
public class Splash extends Activity {
public static Context context;
public static final String LOG_TAG = "Splash ";
#Override
public void onCreate(Bundle bundle)
{
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onCreate...");
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this)); // catch unexpected error
super.onCreate(bundle);
context = this;
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "MainView starts...");
Intent mainIntent = new Intent(Splash.this, MainView.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
#Override
protected void onResume() {
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onResume ...");
super.onResume();
}
}
MainView.class
public class MainView extends Activity {
Context context;
String LOG_TAG = "MainView ";
#Override
public void onCreate(Bundle savedInstanceState)
{
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onCreate ...");
super.onCreate(savedInstanceState);
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
context = this;
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "LoginView starts...");
Intent loginIntent = new Intent(Splash.this, LoginView.class);
Splash.this.startActivity(loginIntent);
Splash.this.finish();
}
#Override
protected void onResume() {
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onResume ...");
super.onResume();
}
#Override
protected void onStart() {
super.onStart();
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onStart ...");
};
#Override
protected void onPause() {
super.onPause();
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onPause ...");
};
#Override
protected void onDestroy() {
MyLog.i(UtilConstants.LOG_TAG_HCE, LOG_TAG + "onDestroy ...");
super.onDestroy();
};
}
Edit 3:****************
I use fragments, maybe this will be the reason, but I am not sure.

The problem is due to a library I used. It works in background and makes the application restarts.
I suspend it when the application goes to background and resume it when it comes foregorund. That is it, the problem is solved.
Thanks you anyway.

Related

Parse Notification works on emulator but not working on real device

I am working on Parse notifications..to send notifications to all users...The below code works well on emulator but the real device cannot get notifications....
Heres my code :
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ParseAnalytics.trackAppOpenedInBackground(getIntent());
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
}
ParseApplication.java
public class ParseApplication extends Application{
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Parse.initialize(this, "[APP ID]", "[CLIENT KEY]");
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
ParseInstallation.getCurrentInstallation().saveInBackground();
}
Receiver.java
public class Receiver extends ParsePushBroadcastReceiver{
#Override
protected void onPushOpen(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
Intent i = new Intent(arg0,MainActivity.class);
i.putExtras(arg1.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
arg0.startActivity(i);
}
AndroidManifest.xml
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="11" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:protectionLevel="signature"
android:name="com.example.parse.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.parse.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:name="com.example.parse.ParseApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
>
<activity
android:name="com.example.parse.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.example.parse.Receiver"
android:exported="false" >
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
</manifest>
You do not have subscription set. If you haven't seen it already please follow the link. Parse Android Push Tutorial
In your Application onCreate:
ParsePush.subscribeInBackground("", new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
} else {
Log.e("com.parse.push", "failed to subscribe for push", e);
}
}
});
As you may be creating your own sample app, pay close attention to the Manifest configuration, ensure you have right package names.
As noted in the Parse docs, they use GCM when Play-Services is available, which actual Android device will have. Where as they use persistent connection on devices without play-service ie. your emulator. Hope this helps.

Parse eventually crashes on android

well i have one problem with parse, but i can´t know why it crashes, i explain it.
Im working with parse, and works ok, but then with no reason, sometimes, when i turn off the mobile or go out of the wifi´s range, the app crashes. i think all its ok, cause i can recieve parse´s notifications with no problem.... well need some help here
The app its a webview
public class MainActivity extends Activity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.activity_main);
Parse.initialize(getApplicationContext(), "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveEventually();
this.myWebView = (WebView) this.findViewById(R.id.web);
myWebView.getSettings().setJavaScriptEnabled(true);
//iniciamos javascript
myWebView.addJavascriptInterface(new WebAppInterface(this),"Android");
//webviewclient para websettings
WebSettings websettings= myWebView.getSettings();
websettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("http://www.aaaaa.com/);
}
#Override
public void onBackPressed(){
if(this.myWebView.canGoBack())
this.myWebView.goBack();
else
super.onBackPressed();
}
private class MyWebViewClient extends WebViewClient{
#SuppressWarnings("unused")
private long loadTime;
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if (Uri.parse(url).getHost().equals("www.aaaaa.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
public class ParseBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "com.parse.ParseBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "received " + intent.getAction());
PushService.startServiceIfRequired(context);
}
}
}
}
and then the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jor.dietd"
android:versionCode="3"
android:versionName="1.2" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.jor.dietd.permission.C2D_MESSAGE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.jor.xxxxxx.permission.C2D_MESSAGE" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.jor.xxxxx.MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.jor.xxxx" />
</intent-filter>
</receiver>
</application>
</manifest>

when the gcm message received, wake lock screen does not work in android 4.1.3

We are developing an Android app.
We are going to wake up Lock-mode, when our App receives a GCM message in Sleep mode.
program works well in Android 4.1 (Jelly-Bean, API 16).
but it does not work in Android 4.3 (Jelly-Bean, API 19).
What is the problem? Please let us know!!!
We programmed as follows:
follow is class files :
public class GCMIntentService extends GCMBaseIntentService {
#Override
protected void onMessage(Context context, Intent intent) {
//...some code omitted...
Intent newIntent;
newIntent = new Intent(context, SomeActivity.class);
newIntent.setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(newIntent);
}
}
public class SomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//...some code omitted...
WakeLockManager.setWakeUp(this);
//...some code omitted...
super.onCreate(savedInstanceState);
}
#Override
protected void onStart() {
//...some code omitted...
super.onStart();
}
#Override
protected void onResume() {
//...some code omitted...
super.onStart();
}
#Override
protected void onPause() {
//...some code omitted...
super.onPause();
}
protected void onStop() {
//...some code omitted...
super.onStop();
};
#Override
protected void onDestroy() {
//...some code omitted...
super.onDestroy();
}
}
public class WakeLockManager {
public static void setWakeUp(Context context) {
Window wnd = ((Activity)context).getWindow();
wnd.addFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
);
}
}
follow is manifest file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<permission
android:name="com.mycompany.myapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mycompany.myapp.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:name="com.mycompany.myapp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppBaseTheme" >
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.mycompany.myapp" />
</intent-filter>
</receiver>
<service android:name="com.mycompany.myapp.GCMIntentService" />
<activity
android:name="com.mycompany.myapp.activities.SomeActivity"
android:configChanges="orientation"
android:label="#string/app_name"
android:theme="#style/NoActionBar"
android:screenOrientation="landscape" >
</activity>
<activity
android:name="com.appkorea.newdriver.activities.MainActivity"
android:configChanges="orientation"
android:label="#string/app_name"
android:theme="#style/AuthTheme"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- ...some code omitted... -->
</application>
</manifest>
in case of API 19, when staring SomeActivity, the onCreate() call-back function is called over 1 times.
I guess several reason as follow.
1. SomeActivity's screenOrientation value is landscape.
So for rotating activity, the onCreate() call-back function is called once.
2. To unlock the lockScreen, the android system show the lock screen,
and rotate the screen into portrait.
because current lock-screen's screenOrientation value is portrait.
At this time, SomeActivity is already created.
so the onCreate call-back function is called once.
3. After unlocking, To show the SomeActivity, the onCreate() is called once.
but now device screenOrientation is portrait,
and SomeActivity's screenOrientation is landscape.
so the onCreate() is called again.
As above that is written, onCreate() is called over 1 times. Unfortunately I don't have a android phone 4.3 now. So I can not show the logcat. Later soon, I will upload the logcat screen capture. Really Thanks to read.

Unable to retrieve ads form airpush

Got myself newly registered on AirPush. I integrated bundle SDK with my App. When I tested it for the first time, I got a couple of impressions. It also shows 2 impressions on dashboard.
Now I am unable to fetch the impressions. Did contact the developer team but to no avail.
My code of MainActivity goes like this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// MMSDK.initialize(this);
// setup a new adView and initialize an adRequest to it.
setupAirPushAdView();
}
#Override
public void onResume() {
super.onResume();
if ((getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
== Configuration.SCREENLAYOUT_SIZE_SMALL) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
try {
if (!appStarted) {
setContentView(R.layout.main);
appStarted = true;
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage() + "\n" + e.getClass().toString() + "\n" + e.getLocalizedMessage() + "\n", Toast.LENGTH_LONG).show();
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
perform();
}
}, 2000);
}
private void perform() {
setContentView(R.layout.app_home);
if (!appStarted) {
// attachAdView();
attachAirPushAdView();
appStarted = true;
}
...
Other Methods :
private void setupAirPushAdView() {
ma = new MA(this, null, false);
airPushAdView = new AdView(this, AdView.BANNER_TYPE_IN_APP_AD, AdView.PLACEMENT_TYPE_INTERSTITIAL, false, false,
AdView.ANIMATION_TYPE_LEFT_TO_RIGHT);
}
private void attachAirPushAdView() {
LinearLayout outerAdLayout = (LinearLayout) findViewById(R.id.externalAdId);
outerAdLayout.addView(airPushAdView);
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="9"/>
<!-- android:theme="#android:style/Theme.DeviceDefault.Light" -->
<!-- android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen" -->
<application android:theme="#style/AppTheme" android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity android:name="MainActivity"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="nosensor"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="differential_activity"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="nosensor"
>
</activity>
<activity android:name="integral_activity"
android:screenOrientation="portrait"
>
</activity>
<activity android:name="equation_activity"
android:screenOrientation="portrait"
>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<!-- android:configChanges="keyboardHidden|orientation"
android:screenOrientation="nosensor" -->
<!-- for all activities -->
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<meta-data android:name="com.gyesa.keanp176500.APPID" android:value="206488" />
<meta-data android:name="com.gyesa.keanp176500.APIKEY" android:value="android*1392147786176500813"/>
<activity android:exported="false" android:name="com.gyesa.keanp176500.AdActivity"
android:configChanges="orientation|screenSize"
android:theme="#android:style/Theme.Translucent" />
<activity android:name="com.gyesa.keanp176500.BrowserActivity"
android:configChanges="orientation|screenSize" />
<activity android:name="com.gyesa.keanp176500.VActivity"
android:configChanges="orientation|screenSize" android:screenOrientation="landscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
<service android:name="com.gyesa.keanp176500.LService" android:exported="false"></service>
<receiver android:name="com.gyesa.keanp176500.BootReceiver" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-feature android:name="android.hardware.microphone" android:required="false" />
</manifest>

How to get action that android application is trying removed/uninstalled from device

When user want to uninstall app from android device, I want user uninstall button click event for that application.
I am getting event of application is removed from device, but I want to show pop-up before application is removed. I am trying to achieve same like doing in 'App Lock' application.
Here is my code to get application removed event through broadcast receiver. But I am totally blank about uninstall button click or before pop-up click. Please guide me in right direction.
Thanks in advance.
public class MainActivity extends Activity {
CustomBroadcastReceiver mApplicationsReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mApplicationsReceiver=new CustomBroadcastReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
filter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
filter.addAction(Intent.ACTION_PACKAGE_VERIFIED);
filter.addAction(Intent.ACTION_PACKAGE_INSTALL);
filter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH);
filter.addAction(Intent.ACTION_DELETE);
filter.addAction(Intent.ACTION_DEFAULT);
filter.addDataScheme("package");
registerReceiver(mApplicationsReceiver, filter);
}
}
public class CustomBroadcastReceiver extends BroadcastReceiver {
/**
* This method captures the event when a package has been removed
*/
#Override
public void onReceive(Context context, Intent intent)
{
System.out.println("Hello from CustomBroadcastReceiver");
if (intent != null) {
String action = intent.getAction();
System.out.println("L1123 : "+action);
if (action.equals(intent.ACTION_PACKAGE_REMOVED)) {
//Log the event capture in the log file ...
System.out.println("The package has been removed");
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bits.uninstallappdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_ADDED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_CHANGED" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_INSTALL" />
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REPLACED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- <receiver android:name=".CustomBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_CHANGED" />
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
</intent-filter>
</receiver> -->
</application>
</manifest>
Please try to get the top activity in the task via ActivityManager, and check if it is the uninstall activity.
Core code:
ComponentName topActivity = mActivityManager.getRunningTasks(1).get(0).topActivity;
String packageName = topActivity.getPackageName();
String className = topActivity.getClassName();
Log.v(TAG, "packageName" + packageName);
Log.v(TAG, "className" + className);
if ("com.android.packageinstaller".equals(packageName)
&& "com.android.packageinstaller.UninstallerActivity".equals(className)) {
//Do anything you want here
}
The following permissions which you are using are granted to system apps only. Make sure you have rooted device to allow such permissions.
<uses-permission android:name="android.permission.BROADCAST_PACKAGE_REMOVED" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.DELETE_PACKAGES" />

Categories

Resources