Close APK installer dialog - java

I work on an enterprise app with a custom update (w/o Google Play). I download and run the update directly from the APK:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
It brings me the installer dialog. After installing the update I relaunch my app with a broadcast receiver:
<receiver android:name=".content.SilentUpdateReceiver">
<intent-filter>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
</intent-filter>
</receiver>
public class SilentUpdateReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
The app opens in the foreground, but the installer dialog remains to stay in the background.
How to close this dialog programmatically?

Related

Your app contains an Implicit Internal Intent vulnerability

Google rejecting my app and i'm having trouble with this security error that appear when i submit app as production release:
Implicit Internal Intent
Your app contains an Implicit Internal Intent vulnerability. Please
see this Google Help Center article for details.
com.mypackage.name.sync.SyncService.onHandleIntent
I applied all recommendations listed here:
Remediation for Implicit PendingIntent Vulnerability
But the error still persists.
My service:
public class SyncService extends IntentService {
protected void onHandleIntent(Intent intent) {
...
Intent i = new Intent("com.mypackage.name.REFRESH");
app.sendBroadcast(i);
...
}
}
Manifest:
<service
android:name=".sync.SyncService"
android:exported="false" >
</service>
The service started in many places in 3 methods like this:
(As recommended by google i did add PendingIntent.FLAG_IMMUTABLE)
Method 1:
Intent intent = new Intent(this, SyncService.class);
PendingIntent pIntent = PendingIntent.getService(this, 0, intent,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis,
SYNC_FREQUENCY, pIntent);
Method 2:
Intent intent = new Intent("com.mypackage.name.REFRESH");
intent = new Intent(getApplicationContext(), SyncService.class);
intent.putExtra("notification_unassigned_sync", true);
startService(intent);
Method 3:
Intent intent = new Intent(getApplicationContext(), SyncService.class);
startService(intent);
Is there anything wrong in my code ? Any recommendations ?
Setting the package name for the intents should fix the issue
intent.setPackage("com.mypackage.name"); // replace with your package name
// from AndroidManifest.xml
How to implement broadcast receiver?
Create a java class named MyBroadcastReceiver.java.
Paste this code in the class
public class MyBroadcastReceiverextends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"I have received.",Toast.LENGHT_SHORT).show();
}
}
Once you have implemented this code, you need to add this to you manifest inside the application tag.
<receiver android:name="com.chatverse.free.TimeBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.mypackage.name.REFRESH"/>
</intent-filter>
</receiver>
Add this code to your activity which opens the first.
IntentFilter mFilter = new IntentFilter("com.mypackage.name.REFRESH");
registerReceiver(new MyBroadcastReceiver(), mFilter);

Broadcast receiver. I want to move to another activity through a broadcast receiver after the charger is disconnected

I'm making a university project where I have to make a charger alarm. I select a ringtone, enter a pin, then plug in a charger. When I plug out, it moves to another activity where the phone starts ringing, the screen is locked and can only proceed if provided a correct pin code.
I'm stuck at the point where I have to move from one activity to another when the charger disconnects.
My code is not working. This is the receiver in the manifest file:
<receiver android:name=".MainActivity$PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
This is the broadcast receiver I made in the MainActivity:
public class PowerConnectionReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "The device is not charging", Toast.LENGTH_SHORT).show();
Intent myintent = new Intent(MainActivity.this,setter.class);
startActivity(myintent);
}
}
Please see:
You cannot receive this through components declared in manifests, only
by explicitly registering for it with...
https://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED
And did you set the corresponding permission?
<uses-permission android:name="android.permission.BATTERY_STATS"/>
Your intent has not correct parameters.
Intent myintent = new Intent(MainActivity.this,setter.class);
Change should be:
Intent myintent = new Intent(context, MainActivity.class)
Make sure your <receiver></receiver> tags are out of the activity tag in Manifest. Place them in application level.

BroadcastReceiver.onReceive not called, but Activity.onNewIntent do. Why?

I am trying to get "DeleteIntent" callback from my notification.
BroadcastReceiver.onReceive not called from PendengIntent, while it works OK when broadcast is launched manually with sendBroadcast(deleteIntent).
It works well when intent target is Activity (hits Activity.onNewIntent) but it is weird to use it since activity goes foreground when message dismissed.
Please, help to get onReceive call to BroadcastReceiver.
It is visible, exported and enabled, located in the same package.
MyCancelReceiver class
public class MyCancelReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
}
}
Android manifest
<activity android:name=".MainActivity" android:launchMode="singleTask">
<MyCancelReceiver" android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="notification_cancelled"/>
</intent-filter>
</receiver>
MainActivity
private MyCancelReceiver mReceiver;
#Override
public void onStart() {
super.onStart();
IntentFilter filter = new IntentFilter();
filter.addAction("notification_cancelled");
mReceiver = new MyCancelReceiver();
registerReceiver(mReceiver, filter);
}
#Override
public void onStop() {
super.onStop();
unregisterReceiver(mReceiver);
}
Notification creation:
Intent cancelIntent = new Intent(getApplicationContext(), MyCancelReceiver.class);
cancelIntent.setAction("notification_cancelled");
cancelIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent deleteIntent = PendingIntent.getActivity(getApplicationContext(), 0, cancelIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification not = new Notification.Builder(getApplicationContext(), Util.ANDROID_CHANNEL_ID)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setSmallIcon(R.drawable.default)
.setAutoCancel(true)
.setDeleteIntent(deleteIntent)
.setContentIntent(contentIntent)
.build();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, not);
Thank you in advance.
It's because when you start your activity, onStart() gets called and you will get a callback in onReceive() as soon as the activity is launched. But when you click your notification onStart won't be called as the activity is already created, In this case, onStart wont be called. Try registering in onResume(), I am pretty sure it will work.
Problem solved using an answer from this thread Click on android notification icon does not trigger broadcast receiver
Explicit intent replaced with implicit one.
Intent cancelIntent = new Intent("notification_canceled");

How can i change incoming call screen of phone in java?

I want to change incoming call screen to my own layout. I used a sample to do this for me. but it sometimes appear(when calling) and sometimes not. I do not know how to solve this problem that my own layout appear every time.
This is my codes.
public class PhoneStateReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
Intent i = new Intent();
i.setClass(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
and this is the manifest.
<receiver android:name="com.example.changescreen.PhoneStateReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Try adding a delay before starting your activity.
The Problem is your activity is being overlapped by the default activity

create app ShortCut on home screen when it installed and open last screen of it

i am trying to create ShortCut when app installed . i am using following code
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.iconname));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.appicon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), DashActivity.class));
sendBroadcast(shortcutintent);enter code here
but in this it always open DashActivity . let say i am using app and app is in c Activity and its running in bacgkground now when i click on shortcut it is opening DashActivity not the opening which where active ie c
The correct way is to listen for a shortcut request from the home screen-- with an intent filter like so in your manifest:
<activity android:name=".ShortCutActivity" android:label="#string/shortcut_label">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Then in the activity that receives the intent, you create an intent for your shortcut and return it as the activity result.
// create shortcut if requested
ShortcutIconResource icon =
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
Intent intent = new Intent();
Intent launchIntent = new Intent(this,ActivityToLaunch.class);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, someNickname());
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
setResult(RESULT_OK, intent);
it is opening DashActivity becoz in this line it is provided to open this.
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), DashActivity.class));
change the name of this Activity with your own Activity which u want to open.
hope this Helps.
From shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), DashActivity.class));
remove the DashActivity.class and provide your applications launcher activity name...so that it will work as you expected.

Categories