How to manually update the HMS Core? - huawei-mobile-services

I have an application in AppGallery available also for the older Huawei phones such as P8 or P9. I noticed that the old phones sometimes catch crash because of outdated HMS Core. I read that HMS Core can update itself automatically but I want do it immediately after I run the app.
It is possible to call it when you use map (I have it on the second screen) but I want inform user about outdated HMS Core as soon as possible.
Is there any sample how to manually call HMS Core version check?

To manually upgrade HMS Core (APK) on a Huawei phone, call the sample code provided below to check whether the version number is earlier than 5.0.0.300.
If so, the HMS Core download screen on AppGallery will appear.
Besides, remember to replace channelId in the URI.
Defined channelId as needed to identify channel data. channelId is the ID of a level-1 channel, that is, the channel from which the user comes.
public static void checkUpdate(Context context){
String version = "5.0.0.300";
String pkg = "com.huawei.hwid";
if (version.compareTo(getVersionCode(pkg, context)) > 0) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("hiapplink://com.huawei.appmarket?appId=C10132067&channelId=xxxxx&referrer=&detailType=0"));
context.startActivity(intent);
}
}
private static String getVersionCode(String packageName, Context context) {
PackageManager packageManager = context.getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}

Yes, it is possible. Here is the provided solution. Everything is based on variable baseVersion. If it's value is higher than your HMS Core version code then you will get mentioned dialog.
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;
import com.huawei.hms.adapter.AvailableAdapter;
import com.huawei.hms.adapter.internal.AvailableCode;
import com.huawei.hms.api.ConnectionResult;
/**
* Check for HMS Update
*/
public class HmsUpdateUtil {
private static final String TAG = "HmsUpdateUtil";
private static boolean isInitialized = false;
private static int versionCheckResult = 12;
/**
* Check if HMS needs update
*
* #param context context
* #return result,0 Available, 1 not Available
*/
public static int isHmsAvailable(Context context) {
if (versionCheckResult == ConnectionResult.SUCCESS ) {
return ConnectionResult.SUCCESS;
}
Log.d(TAG, "isInitialized is:" + isInitialized);
if (isInitialized) {
return 1;
}
// minimum HMS version, if less than this version, result will not be 0
int baseVersion = 50000300;
AvailableAdapter availableAdapter = new AvailableAdapter(baseVersion);
int result = availableAdapter.isHuaweiMobileServicesAvailable(context);
Log.i(TAG, "HMS update result is: " + result);
isInitialized = true;
if (result == ConnectionResult.SUCCESS) {
Log.i(TAG, "HMS is avaiable");
} else {
if (availableAdapter.isUserResolvableError(result)) {
resolution(availableAdapter, context);
} else {
Log.e(TAG, "HMS is not avaiable " + AvailableCode.ERROR_NO_ACTIVITY);
}
}
versionCheckResult = result;
return result;
}
private static void resolution(AvailableAdapter availableAdapter, Context context) {
Log.i(TAG, "HMS update start :");
Activity activity = findActivity(context);
if (activity == null) {
Log.e(TAG, "HMS is not available" + AvailableCode.ERROR_NO_ACTIVITY);
return;
}
// this method will be call upgrade dialog box.
availableAdapter.startResolution(activity, new AvailableAdapter.AvailableCallBack() {
#Override
public void onComplete(int result) {
if (result == AvailableCode.SUCCESS) {
versionCheckResult = result;
Log.i(TAG, "HMS update start success");
} else {
Log.e(TAG, "HMS update failed: " + result);
isInitialized = false;
}
}
});
}
/**
* Get Activity by Context
* #param context context
* #return Activity
*/
public static Activity findActivity(Context context) {
Activity activity = null;
if (context instanceof Activity) {
return (Activity) context;
}
if (context instanceof ContextWrapper) {
ContextWrapper wrapper = (ContextWrapper) context;
return findActivity(wrapper.getBaseContext());
} else {
return activity;
}
}
}

Related

Unity IAPurchase Revoke on Google Store

I am using a non-consumable product to switch off ads for good, after purchasing.
When users buy pro everything works fine. Ads are switched off, also autorenewal in google store works fine after the user reinstalls the app.
My problem starts when the user wants a refund. After all, will be processed by Google and the customer will get his money. I don't know how where to subscribe to my function to turn off the pro version again on the users' app.
I am using IAPManager:
public class IAPManager : MonoBehaviour, IStoreListener
{
public List<Product> ProductList = new List<Product>();
public const string Pro = "pro";
private const string _androidPostFix = "_android";
[SerializeField] I18NText purchaseProBtnText;
public IAPManager Instance;
private void Awake()
{
if (Instance == null) Instance = this;
else Destroy(gameObject);
Init();
EventManager.PurchaseProduct += PurchaseProduct;
EventManager.GetProLocalizedPriceString += GetProLocalPriceString;
}
private void OnDestroy()
{
EventManager.PurchaseProduct -= PurchaseProduct;
EventManager.GetProLocalizedPriceString -= GetProLocalPriceString;
}
public List<Product> AllProducts { get { return m_Controller.products.all.ToList(); } }
private IStoreController m_Controller;
private ITransactionHistoryExtensions m_TransactionHistoryExtensions;
private IGooglePlayStoreExtensions m_GooglePlayStoreExtensions;
private bool m_IsGooglePlayStoreSelected;
private bool m_PurchaseInProgress;
#if RECEIPT_VALIDATION
private CrossPlatformValidator validator;
#endif
private string GetProLocalPriceString()
{
#if UNITY_ANDROID && !UNITY_EDITOR
foreach (Product product in ProductList)
{
if (product.metadata.localizedTitle == Pro)
{
return product.metadata.localizedPriceString;
}
}
return null;
#elif UNITY_EDITOR
foreach (Product product in ProductList)
{
if (product.metadata.localizedTitle == "Fake title for pro")
{
return product.metadata.localizedPriceString;
}
}
return null;
#endif
}
public void Init()
{
var module = StandardPurchasingModule.Instance();
module.useFakeStoreUIMode = FakeStoreUIMode.StandardUser;
var builder = ConfigurationBuilder.Instance(module);
// Set this to true to enable the Microsoft IAP simulator for local testing.
builder.Configure<IMicrosoftConfiguration>().useMockBillingSystem = false;
m_IsGooglePlayStoreSelected = Application.platform == RuntimePlatform.Android && module.appStore == AppStore.GooglePlay;
#if AGGRESSIVE_INTERRUPT_RECOVERY_GOOGLEPLAY
// For GooglePlay, if we have access to a backend server to deduplicate purchases, query purchase history
// when attempting to recover from a network-interruption encountered during purchasing. Strongly recommend
// deduplicating transactions across app reinstallations because this relies upon the on-device, deletable
// TransactionLog database.
builder.Configure<IGooglePlayConfiguration>().aggressivelyRecoverLostPurchases = true;
// Use purchaseToken instead of orderId for all transactions to avoid non-unique transactionIDs for a
// single purchase; two ProcessPurchase calls for one purchase, differing only by which field of the receipt
// is used for the Product.transactionID. Automatically true if aggressivelyRecoverLostPurchases is enabled
// and this API is not called at all.
builder.Configure<IGooglePlayConfiguration>().UsePurchaseTokenForTransactionId(true);
#endif
//builder.AddProduct(Pro, ProductType.NonConsumable);
builder.AddProduct(Pro, ProductType.NonConsumable, new IDs
{
{Pro, GooglePlay.Name},
//{Pro+_iosPostFix, AppleAppStore.Name}
}
);
#if INTERCEPT_PROMOTIONAL_PURCHASES
// On iOS and tvOS we can intercept promotional purchases that come directly from the App Store.
// On other platforms this will have no effect; OnPromotionalPurchase will never be called.
builder.Configure<IAppleConfiguration>().SetApplePromotionalPurchaseInterceptorCallback(OnPromotionalPurchase);
Debug.Log("Setting Apple promotional purchase interceptor callback");
#endif
#if RECEIPT_VALIDATION
string appIdentifier;
#if UNITY_5_6_OR_NEWER
appIdentifier = Application.identifier;
#else
appIdentifier = Application.bundleIdentifier;
#endif
try
{
validator = new CrossPlatformValidator(GooglePlayTangle.Data(), AppleTangle.Data(), appIdentifier);
}
catch (NotImplementedException exception)
{
Debug.Log("Cross Platform Validator Not Implemented: " + exception);
}
#endif
// Now we're ready to initialize Unity IAP.
UnityPurchasing.Initialize(this, builder);
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
m_Controller = controller;
m_TransactionHistoryExtensions = extensions.GetExtension<ITransactionHistoryExtensions>();
m_GooglePlayStoreExtensions = extensions.GetExtension<IGooglePlayStoreExtensions>();
#if SUBSCRIPTION_MANAGER
Dictionary<string, string> introductory_info_dict = m_AppleExtensions.GetIntroductoryPriceDictionary();
#endif
// Sample code for expose product sku details for apple store
//Dictionary<string, string> product_details = m_AppleExtensions.GetProductDetails();
Debug.Log("Available items:");
foreach (var item in controller.products.all)
{
if (item.availableToPurchase)
{
Debug.Log(string.Join(" - ",
new[]
{
item.metadata.localizedTitle,
item.metadata.localizedDescription,
item.metadata.isoCurrencyCode,
item.metadata.localizedPrice.ToString(),
item.metadata.localizedPriceString,
item.transactionID,
item.receipt
}));
ProductList.Add(item);
#if INTERCEPT_PROMOTIONAL_PURCHASES
// Set all these products to be visible in the user's App Store according to Apple's Promotional IAP feature
// https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StoreKitGuide/PromotingIn-AppPurchases/PromotingIn-AppPurchases.html
m_AppleExtensions.SetStorePromotionVisibility(item, AppleStorePromotionVisibility.Show);
#endif
#if SUBSCRIPTION_MANAGER
// this is the usage of SubscriptionManager class
if (item.receipt != null) {
if (item.definition.type == ProductType.Subscription) {
if (checkIfProductIsAvailableForSubscriptionManager(item.receipt)) {
string intro_json = (introductory_info_dict == null || !introductory_info_dict.ContainsKey(item.definition.storeSpecificId)) ? null : introductory_info_dict[item.definition.storeSpecificId];
SubscriptionManager p = new SubscriptionManager(item, intro_json);
SubscriptionInfo info = p.getSubscriptionInfo();
Debug.Log("product id is: " + info.getProductId());
Debug.Log("purchase date is: " + info.getPurchaseDate());
Debug.Log("subscription next billing date is: " + info.getExpireDate());
Debug.Log("is subscribed? " + info.isSubscribed().ToString());
Debug.Log("is expired? " + info.isExpired().ToString());
Debug.Log("is cancelled? " + info.isCancelled());
Debug.Log("product is in free trial peroid? " + info.isFreeTrial());
Debug.Log("product is auto renewing? " + info.isAutoRenewing());
Debug.Log("subscription remaining valid time until next billing date is: " + info.getRemainingTime());
Debug.Log("is this product in introductory price period? " + info.isIntroductoryPricePeriod());
Debug.Log("the product introductory localized price is: " + info.getIntroductoryPrice());
Debug.Log("the product introductory price period is: " + info.getIntroductoryPricePeriod());
Debug.Log("the number of product introductory price period cycles is: " + info.getIntroductoryPricePeriodCycles());
} else {
Debug.Log("This product is not available for SubscriptionManager class, only products that are purchase by 1.19+ SDK can use this class.");
}
} else {
Debug.Log("the product is not a subscription product");
}
} else {
Debug.Log("the product should have a valid receipt");
}
#endif
}
}
}
public void Restore()
{
m_GooglePlayStoreExtensions.RestoreTransactions(OnTransactionsRestored);
Debug.Log("Restore method");
}
private void OnTransactionsRestored(bool success)
{
Debug.Log("Transactions restored." + success);
}
public void OnPurchaseFailed(Product item, PurchaseFailureReason r)
{
Debug.Log("Purchase failed: " + item.definition.id);
Debug.Log(r);
// Detailed debugging information
Debug.Log("Store specific error code: " + m_TransactionHistoryExtensions.GetLastStoreSpecificPurchaseErrorCode());
if (m_TransactionHistoryExtensions.GetLastPurchaseFailureDescription() != null)
{
Debug.Log("Purchase failure description message: " +
m_TransactionHistoryExtensions.GetLastPurchaseFailureDescription().message);
}
m_PurchaseInProgress = false;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
Debug.Log("Billing failed to initialize!");
switch (error)
{
case InitializationFailureReason.AppNotKnown:
Debug.LogError("Is your App correctly uploaded on the relevant publisher console?");
break;
case InitializationFailureReason.PurchasingUnavailable:
// Ask the user if billing is disabled in device settings.
Debug.Log("Billing disabled!");
break;
case InitializationFailureReason.NoProductsAvailable:
// Developer configuration error; check product metadata.
Debug.Log("No products available for purchase!");
break;
}
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
{
Debug.Log("Purchase OK: " + e.purchasedProduct.definition.id);
Debug.Log("Receipt: " + e.purchasedProduct.receipt);
m_PurchaseInProgress = false;
#if RECEIPT_VALIDATION // Local validation is available for GooglePlay, and Apple stores
if (m_IsGooglePlayStoreSelected ||
Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer ||
Application.platform == RuntimePlatform.tvOS) {
try {
var result = validator.Validate(e.purchasedProduct.receipt);
Debug.Log("Receipt is valid. Contents:");
foreach (IPurchaseReceipt productReceipt in result) {
Debug.Log(productReceipt.productID);
Debug.Log(productReceipt.purchaseDate);
Debug.Log(productReceipt.transactionID);
GooglePlayReceipt google = productReceipt as GooglePlayReceipt;
if (null != google) {
Debug.Log(google.purchaseState);
Debug.Log(google.purchaseToken);
}
AppleInAppPurchaseReceipt apple = productReceipt as AppleInAppPurchaseReceipt;
if (null != apple) {
Debug.Log(apple.originalTransactionIdentifier);
Debug.Log(apple.subscriptionExpirationDate);
Debug.Log(apple.cancellationDate);
Debug.Log(apple.quantity);
}
// For improved security, consider comparing the signed
// IPurchaseReceipt.productId, IPurchaseReceipt.transactionID, and other data
// embedded in the signed receipt objects to the data which the game is using
// to make this purchase.
}
}
catch (IAPSecurityException ex)
{
Debug.Log("Invalid receipt, not unlocking content. " + ex);
return PurchaseProcessingResult.Complete;
}
catch (NotImplementedException exception)
{
Debug.Log("Cross Platform Validator Not Implemented: " + exception);
}
}
#endif
// Unlock content from purchases here.
if (e.purchasedProduct.definition.id == Pro)
{
//PlayerPrefs.SetInt("pro", 1);
Debug.Log("PURCHASED PRODUCT!");
//AdsManager.Instance.ProActive = true;
AdsManager.Instance.HideAdsPro();
//EventBroker.CallOnProBought();
}
#if USE_PAYOUTS
if (e.purchasedProduct.definition.payouts != null) {
Debug.Log("Purchase complete, paying out based on defined payouts");
foreach (var payout in e.purchasedProduct.definition.payouts) {
Debug.Log(string.Format("Granting {0} {1} {2} {3}", payout.quantity, payout.typeString, payout.subtype, payout.data));
}
}
#endif
#if DELAY_CONFIRMATION
StartCoroutine(ConfirmPendingPurchaseAfterDelay(e.purchasedProduct));
return PurchaseProcessingResult.Pending;
#else
return PurchaseProcessingResult.Complete;
#endif
}
/// <summary>
/// Call this method to start purchase product.
/// </summary>
/// <param name="productID">Product ID from products on Google Dev Dashboard</param>
public void PurchaseProduct(string productID)
{
Debug.Log("PurchaseProduct");
if (m_PurchaseInProgress == true)
{
Debug.Log("Please wait, purchase in progress");
return;
}
if (m_Controller == null)
{
Debug.LogError("Purchasing is not initialized");
return;
}
if (m_Controller.products.WithID(productID) == null)
{
Debug.LogError("No product has id " + productID);
return;
}
m_PurchaseInProgress = true;
m_Controller.InitiatePurchase(m_Controller.products.WithID(productID), "developerPayload");
}
}
I found a solution myself.
I checked product.hasRecipt to get info if the product is bought.
So the receipt disappears after a successful revoke process in the google app store.
And every time the app is restarted this bool is sent to the app during IAP initialization.

Pass data from android to flutter

I have added my Android side code:
I know that I need to use a platform channel to pass data,I am unable to figure out:
import io.flutter.embedding.android.FlutterActivity;
public class MainActivity extends AppCompatActivity {
private Button Btn;
// Intent defaultFlutter=FlutterActivity.createDefaultIntent(activity);
String path;
private Button bt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Btn = findViewById(R.id.btn);
isStoragePermissionGranted();
Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
path=takeScreenshot();
// activity.startActivity(defaultFlutter);
}
});
//write flutter xode here
//FlutterActivity.createDefaultIntent(this);
}
private String takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
Log.d("path",mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
return mPath;
///openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or DOM
e.printStackTrace();
return "Error";
}
}
public boolean isStoragePermissionGranted() {
String TAG = "Storage Permission";
if (Build.VERSION.SDK_INT >= 23) {
if (this.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG, "Permission is granted");
return true;
} else {
Log.v(TAG, "Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
}
}
I will receive a file from the android side, upon receiving I need to display it in a flutter. I also need to use cached engine for transferring data as normally it would cause a delay
You can use the cached engine, this will help me cover up for the delay.
Then you can add a invoke method onpressed that you can send method name and the data you want to pass.
On flutter side,you can create a platform and invoke method through which you can receive requirements and further process it,

How to setting firebase remote config for unity?

lets to the point, so i want to make some configuration with my game to show Ads but I avoid to update game version too much, because of that I choose firebase remote config with this I can update setting/configuration without update the game version.
there is document for this but not very clear for newbie like me, you can check it here https://firebase.google.com/docs/remote-config/use-config-unity
I already make the script like on doc, but I don't understand how its work on show data because error string format, which is what I know on this firebase console is int format
the script like this :
public static _FirebaseRemoteConfig instance;
private void Awake()
{
instance = this;
}
Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
// Use this for initialization
void Start()
{
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task =>
{
dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available)
{
InitializeFirebase();
}
else
{
Debug.LogError(
"Could not resolve all Firebase dependencies: " + dependencyStatus);
}
});
}
public void InitializeFirebase()
{
System.Collections.Generic.Dictionary<string, object> defaults =
new System.Collections.Generic.Dictionary<string, object>();
defaults.Add("config_test_string", "default local string");
defaults.Add("config_test_int", 1);
defaults.Add("config_test_float", 1.0);
defaults.Add("config_test_bool", false);
Firebase.RemoteConfig.FirebaseRemoteConfig.SetDefaults(defaults);
Debug.Log("Remote config ready!");
}
public void FetchFireBase()
{
FetchDataAsync();
}
public void ShowData()
{
Debug.Log("maxCountToShowAdmob: " +
Firebase.RemoteConfig.FirebaseRemoteConfig.GetValue("MaxCountShowIntersitialAds").LongValue);
}
// Start a fetch request.
public Task FetchDataAsync()
{
Debug.Log("Fetching data...");
System.Threading.Tasks.Task fetchTask = Firebase.RemoteConfig.FirebaseRemoteConfig.FetchAsync(
TimeSpan.Zero);
return fetchTask.ContinueWith(FetchComplete);
}
void FetchComplete(Task fetchTask)
{
if (fetchTask.IsCanceled)
{
Debug.Log("Fetch canceled.");
}
else if (fetchTask.IsFaulted)
{
Debug.Log("Fetch encountered an error.");
}
else if (fetchTask.IsCompleted)
{
Debug.Log("Fetch completed successfully!");
}
var info = Firebase.RemoteConfig.FirebaseRemoteConfig.Info;
switch (info.LastFetchStatus)
{
case Firebase.RemoteConfig.LastFetchStatus.Success:
Firebase.RemoteConfig.FirebaseRemoteConfig.ActivateFetched();
Debug.Log(String.Format("Remote data loaded and ready (last fetch time {0}).",
info.FetchTime));
break;
case Firebase.RemoteConfig.LastFetchStatus.Failure:
switch (info.LastFetchFailureReason)
{
case Firebase.RemoteConfig.FetchFailureReason.Error:
Debug.Log("Fetch failed for unknown reason");
break;
case Firebase.RemoteConfig.FetchFailureReason.Throttled:
Debug.Log("Fetch throttled until " + info.ThrottledEndTime);
break;
}
break;
case Firebase.RemoteConfig.LastFetchStatus.Pending:
Debug.Log("Latest Fetch call still pending.");
break;
}
}

How to pass GPS (lat,lon etc) to Android services

I'm trying to build a background service which is able to log the data and save it into a SQLite database at a specific time interval without user consent.
So far:
I was able to start the services and toast current LastKnownLocation. This just to see if the variable is okay before I insert it to SQLite table.
However, if the GPS is not active the application will crash.
What's wrong with my code, and how to make it auto change when location changes?
public class SpycareServices extends Service {
/*Location Listener Declaration*/
PhoneInfo myPhone;
Util myFunction;
private LocationListener listener;
private LocationManager locationManager;
boolean gps_enabled = false;
boolean network_enabled = false;
Location net_loc = null, gps_loc = null, finalLoc = null;
public double longitude,latitude;
/*Declare SharedPref StartService*/
public SpycareServices(){
}
/*Declare constructor for location listener*/
public SpycareServices(Context context){
locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate(){
/*Instantiate listener*/
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
//Check which provider active
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if(gps_enabled)
{
gps_loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if(network_enabled)
{
net_loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//net_loc = locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,3000,0,listener);
}
/*If both gps and network has value, choose the better one*/
if (gps_loc != null && net_loc != null) {
//smaller the number more accurate result will
if (gps_loc.getAccuracy() > net_loc.getAccuracy()){
finalLoc = net_loc;
//
}
else{
finalLoc = gps_loc;
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,listener);
}
} else {
if (gps_loc != null) {
finalLoc = gps_loc;
} else if (net_loc != null) {
finalLoc = net_loc;
}
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId){
Toast.makeText(getApplicationContext(),"Services Has Been Started!",Toast.LENGTH_SHORT).show();
//Instance util to get timestamp
myFunction = new Util(this);
Toast.makeText(getApplicationContext(),"My time "+ myFunction.getTimestamp(this),Toast.LENGTH_SHORT).show();
//Instance Imei
myPhone = new PhoneInfo(this);
Toast.makeText(getApplicationContext(),"My Imei "+ myPhone.betaGetImei(this),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"My Location Provider is "+ finalLoc.getProvider(),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"My Longitude is "+ finalLoc.getLongitude(),Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(),"My Latitude is "+ finalLoc.getLatitude(),Toast.LENGTH_SHORT).show();
return START_STICKY;
}
#Override
public void onDestroy(){
Toast.makeText(getApplicationContext(),"Services Has Been Destroyed!",Toast.LENGTH_SHORT).show();
//super.onDestroy();
//Prevent memory leaks by deregister listener when destroyed
if(locationManager!=null){
locationManager.removeUpdates(listener);
}
}

What am I supposed to use in place of "Plugin" for Phonegap 2.9?

I am trying to implement the WebIntent plugin for Phonegap as part of my Android app.
I have downloaded the latest version of WebIntent, within this week, and it has not been updated for about half a year, so I think it is as up to date as possible. I am using version 2.9 of Phonegap.
WebIntent is not working for me, and in my Eclipse interface, it says that the class "Plugin" is deprecated:
This answer here on Stack Overflow indicates that I should use "CordovaPlugin" instead, but Eclipse seems to like that even less:
I'm not sure if this is the reason WebIntent is failing for me, but it looks to be a very likely suspect. What do I do to clear this file of any errors?
Also, I don't know if this is related, but there is another line in the code that triggers another "depracated" warning:
So it may be that WebIntent needs some more general fix or something.
In any case, I just want to get WebIntent working. Advice on how to do that, whether it involves fixing this Plugin error or not, would be most appreciated.
Update: I have tried to edit the code using "CordovaPlugin", but I am still getting errors.
The lines I am getting errors on are:
... and:
This is the whole file:
package com.borismus.webintent;
import java.util.HashMap;
import java.util.Map;
import org.apache.cordova.DroidGap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.text.Html;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.apache.cordova.api.CallbackContext;
/**
* WebIntent is a PhoneGap plugin that bridges Android intents and web
* applications:
*
* 1. web apps can spawn intents that call native Android applications. 2.
* (after setting up correct intent filters for PhoneGap applications), Android
* intents can be handled by PhoneGap web applications.
*
* #author boris#borismus.com
*
*/
public class WebIntent extends CordovaPlugin {
private String onNewIntentCallback = null;
/**
* Executes the request and returns PluginResult.
*
* #param action
* The action to execute.
* #param args
* JSONArray of arguments for the plugin.
* #param callbackContext
* The callbackContext used when calling back into JavaScript.
* #return boolean
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
try {
if (action.equals("startActivity")) {
if (args.length() != 1) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
// Parse the arguments
JSONObject obj = args.getJSONObject(0);
String type = obj.has("type") ? obj.getString("type") : null;
Uri uri = obj.has("url") ? Uri.parse(obj.getString("url")) : null;
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
Map<String, String> extrasMap = new HashMap<String, String>();
// Populate the extras if any exist
if (extras != null) {
JSONArray extraNames = extras.names();
for (int i = 0; i < extraNames.length(); i++) {
String key = extraNames.getString(i);
String value = extras.getString(key);
extrasMap.put(key, value);
}
}
startActivity(obj.getString("action"), uri, type, extrasMap);
callbackContext.success();
return true;
} else if (action.equals("hasExtra")) {
if (args.length() != 1) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
String extraName = args.getString(0);
PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
return true;
} else if (action.equals("getExtra")) {
if (args.length() != 1) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
String extraName = args.getString(0);
if (i.hasExtra(extraName)) {
PluginResult res = new PluginResult(PluginResult.Status.OK, i.hasExtra(extraName));
callbackContext.sendPluginResult(res);
return true;
} else {
PluginResult res = new PluginResult(PluginResult.Status.ERROR);
callbackContext.sendPluginResult(res);
return false;
}
} else if (action.equals("getUri")) {
if (args.length() != 0) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
Intent i = ((DroidGap)this.cordova.getActivity()).getIntent();
String uri = i.getDataString();
callbackContext.success(uri);
return true;
} else if (action.equals("onNewIntent")) {
if (args.length() != 0) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
this.onNewIntentCallback = callbackContext;
PluginResult res = new PluginResult(PluginResult.Status.NO_RESULT);
res.setKeepCallback(true);
callbackContext.sendPluginResult(res);
return true;
} else if (action.equals("sendBroadcast"))
{
if (args.length() != 1) {
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
}
// Parse the arguments
JSONObject obj = args.getJSONObject(0);
JSONObject extras = obj.has("extras") ? obj.getJSONObject("extras") : null;
Map<String, String> extrasMap = new HashMap<String, String>();
// Populate the extras if any exist
if (extras != null) {
JSONArray extraNames = extras.names();
for (int i = 0; i < extraNames.length(); i++) {
String key = extraNames.getString(i);
String value = extras.getString(key);
extrasMap.put(key, value);
}
}
sendBroadcast(obj.getString("action"), extrasMap);
callbackContext.success();
return true;
}
PluginResult res = new PluginResult(PluginResult.Status.INVALID_ACTION);
callbackContext.sendPluginResult(res);
return false;
} catch (JSONException e) {
callbackContext.error(e.getMessage());
return false;
}
}
#Override
public void onNewIntent(Intent intent) {
if (this.onNewIntentCallback != null) {
this.onNewIntentCallback.success(intent.getDataString());
}
}
void startActivity(String action, Uri uri, String type, Map<String, String> extras) {
Intent i = (uri != null ? new Intent(action, uri) : new Intent(action));
if (type != null && uri != null) {
i.setDataAndType(uri, type); //Fix the crash problem with android 2.3.6
} else {
if (type != null) {
i.setType(type);
}
}
for (String key : extras.keySet()) {
String value = extras.get(key);
// If type is text html, the extra text must sent as HTML
if (key.equals(Intent.EXTRA_TEXT) && type.equals("text/html")) {
i.putExtra(key, Html.fromHtml(value));
} else if (key.equals(Intent.EXTRA_STREAM)) {
// allowes sharing of images as attachments.
// value in this case should be a URI of a file
i.putExtra(key, Uri.parse(value));
} else if (key.equals(Intent.EXTRA_EMAIL)) {
// allows to add the email address of the receiver
i.putExtra(Intent.EXTRA_EMAIL, new String[] { value });
} else {
i.putExtra(key, value);
}
}
((DroidGap)this.cordova.getActivity()).startActivity(i);
}
void sendBroadcast(String action, Map<String, String> extras) {
Intent intent = new Intent();
intent.setAction(action);
for (String key : extras.keySet()) {
String value = extras.get(key);
intent.putExtra(key, value);
}
((DroidGap)this.cordova.getActivity()).sendBroadcast(intent);
}
}
If you are using Phonegap 2.9, so the "Plugin" needs to be "CordovaPlugin".
But if you see, the return type of both are different. "Plugin" expects return as a "PluginResult" instance, where as "CordovaPlugin" expects return as a boolean value, and the callback is triggered using "CallbackContext" instance. some thing like
callbackContext.success("Message : OK");
or
callbackContext.error("Expected one non-empty string argument.");
You can have a deep look into this over the docs here http://docs.phonegap.com/en/2.9.0/guide_plugin-development_android_index.md.html#Developing%20a%20Plugin%20on%20Android