My application I'm making must have to able to download files from Google Drive. So, The app should check that user has Google Drive. One class named DriveDetector is role of checking this facts. Actually, And basically all of Android Phone have Google Application that including Google Drive. Android users cannot remove the Google Drive completely, but they're able to be Google Drive invisible. HOW TO CHECK GOOGLE DRIVE IS ACTIVATED ON THE USER'S PHONE? Thanks to read my question post.
import android.content.Context;
public class DriveDetector {
private Context context;
public DriveDetector (Context context) {
this.context = context;
}
public boolean hasGoogleDrive() {
}
}
you can check if application is already installed in phone or not by following:
public static boolean isAppInstalled(Context context, String packageName) {
try {
context.getPackageManager().getApplicationInfo(packageName, 0);
return true;
}
catch (PackageManager.NameNotFoundException e) {
return false;
}
}
you can call this method to check for google drive like this:
boolean hasDrive = isAppInstalled(context, "com.google.android.apps.doc");
Related
I am using firebase to send notifications to the application and based on the notification the user will be directed to some activity. so far so good, the problem I am having is that if the application is running on background the activity that will be shown is the main activity but I can not quite managed it to do it.
I believe there ar several questions like this, but do not quite get the answer I am looking for.
Any help or suggestion would be great, thanks
try
/**Flag to determine if the Activity is visible*/
private static boolean activityVisible;
private static boolean activityDestroy;
/**Is the application actually visible on the mobile screen*/
public static boolean isActivityVisible(){
return activityVisible;
}
/**Is the application actually destroyed*/
public static boolean isActivityDestroy(){
return activityDestroy;
}
/**Is the application actually destroyed*/
public static void activityDestroy(boolean isDestroy){
activityDestroy = isDestroy;
}
/**Is the application actually in the background*/
public static boolean isActivityInBackground(){
return !activityVisible;
}
/**Change the state of the Application to resume*/
public static void activityResumed() {
activityVisible = true;
}
/**Change the state of the Application to paused*/
public static void activityPaused() {
activityVisible = false;
}
and then for checking
Application app = ((Application)getApplicationContext());
boolean visible = app.isActivityVisible();
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen.
You could use importance field of ActivityManager.
Below the method for Xamarin using by C#, but it's pretty similiar to Java.
P.S.: If you want I could port that code to Java
public static Importance GetAppState(Context context, string packageName)
{
try
{
var manager = (ActivityManager)context.GetSystemService(Context.ActivityService);
var processes = manager.RunningAppProcesses;
if (processes != null)
foreach (var process in processes)
if (process.ProcessName.Equals(packageName))
return process.Importance;
}
catch
{
}
return Importance.Gone;
}
I implement a drawing route app on GoogleMaps and that is working but first I need to check for GoogleMaps availablity on Android or not. If the app does not exist, the user should be directed to Google Play Store to install GoogleMaps.
How can I prompt the user to install it?
Try this:
private boolean isPackageInstalled(String packagename, PackageManager packageManager) {
try {
packageManager.getPackageInfo(packagename, 0);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
It attempts to fetch information about the package whose name you passed in. Failing that, if a NameNotFoundException was thrown, it means that no package with that name is installed, so we return false.
in your case
public void someMethod() {
// ...
String googleMapsPackageName = "com.google.android.apps.maps"
PackageManager pm = context.getPackageManager();
boolean isInstalled = isPackageInstalled(googleMapsPackageName, pm);
if(isInstalled)
// go to maps
else
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + googleMapsPackageName)));
// ...
}
I implemented install referral tracking in my app from:
https://developers.google.com/analytics/devguides/collection/android/v4/campaigns#google-play-campaigns
my referral receiver:
public class ReferrerReceiver extends BroadcastReceiver
{
private static final ObservableChanged _observable = new ObservableChanged();
//--------------------------------------------------------------------------
public static Observable getObservable()
{
return _observable;
}
//--------------------------------------------------------------------------
public static String getReferrer(Context context)
{
// Return any persisted referrer value or null if we don't have a referrer.
return context.getSharedPreferences("referrer", Context.MODE_PRIVATE).getString("referrer", null);
}
//--------------------------------------------------------------------------
public ReferrerReceiver()
{
Logger.log(null, "ReferrerReceiver.ReferrerReceiver()");
}
//--------------------------------------------------------------------------
#Override public void onReceive(Context context, Intent intent)
{
Logger.log(context, "ReferrerReceiver.onReceive(Context, Intent)", intent);
try
{
// Make sure this is the intent we expect - it always should be.
if ((null != intent) && (intent.getAction().equals("com.android.vending.INSTALL_REFERRER")))
{
// This intent should have a referrer string attached to it.
String rawReferrer = intent.getStringExtra("referrer");
if (null != rawReferrer)
{
// The string is usually URL Encoded, so we need to decode it.
String referrer = URLDecoder.decode(rawReferrer, "UTF-8");
// Log the referrer string.
Logger.log(context,
"ReferrerReceiver.onReceive(Context, Intent)" +
"\nRaw referrer: " + rawReferrer +
"\nReferrer: " + referrer);
// Persist the referrer string.
context.getSharedPreferences("referrer", Context.MODE_PRIVATE).
edit().putString("referrer", referrer).commit();
// Let any listeners know about the change.
_observable.notifyObservers(referrer);
}
}
}
catch (Exception e)
{
Logger.log(context, e.toString());
}
}
//**************************************************************************
protected static class ObservableChanged extends Observable
{
//----------------------------------------------------------------------
#Override public boolean hasChanged()
{
return true;
}
}
}
but referrer attribute is getting broadcasted by the Play Store to my app after every repeated install(uninstall and install on same device).
for a quick test install this app from play store(not mine) from this link:
https://play.google.com/store/apps/details?id=fr.simon.marquis.installreferrer&referrer=myReferrerValue
you will get referral value = "myReferrerValue" on every first launch of repeated install(uninstall and install on same device).
My questions are:
Does the play store send referral broadcast even when app are installed on the same device repeated times?
Shouldn't referrers only be broadcasted once per device?
Yes the play store will send the referrer every time the app is installed using a link that contains parameters.
If you care about uniqueness, you need some backend to verify that yourself. This is how e.g adjust does it. The referrer is stored in the Receiver and sent to a backend at some point in the future.
If you think of it it makes a lot of sense: The only thing that the Play Store App does is taking the referrer parameters from the url and delegating it back to the installed app. There is basically no logic involved here.
Also, the developers at google don't know whether you are interested in the uniqueness or not, so they will not prevent you from counting installs multiple times, if you want. (AFAIK the statistics panel in the Play Developer Console does filter out duplicates, but they're still delivered to your app)
Im currently developping an app in China. I have a small problem, on the chinese phones Google Maps is initially not installed but i need it for the application. For the users its no problem to install it but i want them to have a choice which market to use.
public void someButtonClicked(View v) {
if (!isGoogleMapsInstalled()){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=com.google.android.apps.maps"));
startActivity(intent);
}
}
public boolean isGoogleMapsInstalled()
{
try
{
ApplicationInfo info = getPackageManager().getApplicationInfo("com.google.android.apps.maps", 0 );
return true;
}
catch(PackageManager.NameNotFoundException e)
{
return false;
}
}
this is what i have and actually it only opens the play store but i cannot decide which store to use.
As last sorry for my bad English.
I am using the Android Billing API V3 for querying purchase information from the play store. I am wondering if it is possible to clear the local cache.
V3 added the local caching feature for the billing API to cut down on network traffic. I have an update service which is run daily which updates my metadata and since I display the costs in my application I wish to flush the local cache and trigger an update in case I have updated prices to ensure the correct price is shown.
Documentation says:
Because the Google Play client now caches In-app Billing information locally on the device, you can use the Version 3 API to query for this information more frequently, for example through a getPurchases call. Unlike with previous versions of the API, many Version 3 API calls will be serviced through cache lookups instead of through a network connection to Google Play, which significantly speeds up the API's response time.
You must know that the android in app purchase one time purchases are allow only one time in the lifetime for one user. you want it again you have to make a request or create a new product in play console. for more go to this link.
https://developer.android.com/google/play/billing/billing_onetime
Unfortunately not, the Google Play client is app doing the caching, and there is not an API exposed to clear the cache.
I don't see why you want to clear the cache though? The Google Play client is notified of any changes, so would invalidate it's cache accordingly. Just assume the calls returned are correct.
Try this :
Add this Application class :
package com.hrupin.cleaner;
import java.io.File;
import android.app.Application;
import android.util.Log;
public class MyApplication extends Application {
private static MyApplication instance;
#Override
public void onCreate() {
super.onCreate();
instance = this;
}
public static MyApplication getInstance() {
return instance;
}
public void clearApplicationData() {
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(new File(appDir, s));
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
}
Then, Make this call from any Activity:
MyApplication.getInstance().clearApplicationData();
Reference :
How to Clear User Data in your Android Application programmatically
Thanks.