I'm trying to insert a button to rate app in my activity, with a toast for if market isn't found. But I'm getting a "Context cannot be resolved to a variable" on Activity.this:
Uri uri = Uri.parse("market://details?id=" + getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(Activity.this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
}
I've also tried:
Toast.makeText(this, "Couldn't launch the market", Toast.LENGTH_LONG).show();
But then I get Multiple markers at this line
- The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
I've made a simple button toast the same way (without try/catch) before, and then it worked fine..
What have I done wrong?
If your class is extending with Activity means use like this
Toast.makeText(ClassName.this, "Couldn't launch the market",Toast.LENGTH_LONG).show();
or
Toast.makeText(getApplicationContext(), "Couldn't launch the market",Toast.LENGTH_LONG).show();
If your Class is extending with Fragment means use like this:
Toast.makeText(getActivity(), "Couldn't launch market",Toast.LENGTH_LONG).show();
Your answer:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LENGTH_LONG).show();
Try:
Toast.makeText(getApplicationContext(), "Couldn't launch the market", Toast.LEGTH_LONG).show();
Try this...
Uri uri = Uri.parse("market://details?id="
+ getApplicationContext().getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't launch the market", Toast.LENGTH_LONG)
.show();
}
});
}
Hope this will help you...
Related
currently i am using this but(to open map location on image click but some device having GoogleMap cant open the intent) the issue is even device has apps to handle the map intent it wont execute that part
any better way to do this?
like option to select from available apps and no apps to handle that then go for browser
private void openMap()
{
String Packagename ="com.google.android.apps.maps";
if (appInstalledOrNot(Packagename))
{
Uri uri = Uri.parse("geo:0,0?q=V.V.P.+Engineering+College+Kalavad+Road+Virda+Vajadi,+Rajkot");
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.setPackage("com.google.android.apps.maps");
startActivity(intent);
}else
{
try {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://goo.gl/maps/ETVvFWw453CoeBHs9"));
startActivity(myIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Toast.makeText(getContext(), "Map application not found", Toast.LENGTH_SHORT).show();
}
}
It's my first project in java.
I want to add wallpaper backup/restore functionality to my app.
Code to backup:
public void btnBackupWallpaper(View view) {
File wallpaper = new File("/data/system/users/0/wallpaper");
if(wallpaper.exists()){
RootCmd.RunRootCmd("cp -f /data/system/users/0/wallpaper /data/local/tmp/wallpaper");
Toast.makeText(getApplicationContext(), "Wallpaper backup completed.",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Wallpaper not found.",
Toast.LENGTH_LONG).show();
}
}
Code to restore
public void btnRestoreWallpaper(View view) {
File wallpaper = new File("/data/local/tmp/wallpaper");
if(wallpaper.exists()){
RootCmd.RunRootCmd("cp /data/local/tmp/wallpaper /data/system/users/0/wallpaper");
RootCmd.RunRootCmd("chmod 0700 /data/system/users/0/wallpaper");
RootCmd.RunRootCmd("chown system.system /data/system/users/0/wallpaper");
RootCmd.RunRootCmd("rm /data/local/tmp/wallpaper");
Toast.makeText(getApplicationContext(), "Wallpaper restore completed.",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Wallpaper backup not found.",
Toast.LENGTH_LONG).show();
}
}
Restore working fine, but backup always says "Wallpaper not found".
File wallpaper = new File("/data/system/users/0/wallpaper");
if(wallpaper.exists()){}
Why is this part of code doesn't work?
Thanks.
My solution is to use RootTools:
public void btnBackupWallpaper(View view) {
if(RootTools.exists("/data/system/users/0/wallpaper")) {
RootCmd.RunRootCmd("cp -f /data/system/users/0/wallpaper /data/local/tmp/wallpaper");
Context context = getApplicationContext();
Toast.makeText(context, context.getString(R.string.wallpaper_backup_completed), Toast.LENGTH_LONG).show();
} else {
Context context = getApplicationContext();
Toast.makeText(context, context.getString(R.string.wallpaper_not_found), Toast.LENGTH_LONG).show();
}
}
I'm trying to send a message to a telegram-app user, but the intent opens only the telegram app - it don't choose a conctact and send the message:
public void shareTelegram(String message)
{
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
waIntent.setPackage("org.telegram.messenger");
if (waIntent != null)
{
waIntent.putExtra(Intent.EXTRA_TEXT, message);//
startActivity(Intent.createChooser(waIntent, "Daniel"));
}
else
{
Toast.makeText(getApplicationContext(), "Telegram is not installed", Toast.LENGTH_SHORT).show();
}
}
Is there a way to send the message completely?
Can I send the message completely without displaying telegram ?
TLSharp is basic implementation of Telegram API on C#. See it here https://github.com/sochix/TLSharp
Try this.
try {
Toast.makeText(getApplicationContext(), "Sharing Via telegram !", Toast.LENGTH_LONG).show();
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("image/*");
waIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);//Check if package exists or not. If not then code
waIntent.setPackage("org.telegram"); //package check whether telegram is installed
waIntent.putExtra(Intent.EXTRA_TEXT, txt.getText().toString());//place your text here
startActivity(Intent.createChooser(waIntent, "Share with"));
}
catch (PackageManager.NameNotFoundException e)
{
Toast.makeText(SingleItemView.this, "telegram not installed", Toast.LENGTH_SHORT).show();
}
This is my code, an attempt to launch an in app purchase when a button is pressed:
purchaseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(purchased.equals("remove_ads")) {
Toast.makeText(getApplicationContext(), "You already own the item.",
Toast.LENGTH_LONG).show();
}
else{
try{
Bundle buyIntentBundle = mservice.getBuyIntent(3, getPackageName(),
"remove_ads", "inapp", "key");
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
try{
startIntentSenderForResult(pendingIntent.getIntentSender(), ///NPE here
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
}
catch(IntentSender.SendIntentException ee){
Toast.makeText(getApplicationContext(), "Error was: " + ee,
Toast.LENGTH_LONG).show();
}
catch(NullPointerException n){
Toast.makeText(getApplicationContext(), "Error was: " + n,
Toast.LENGTH_LONG).show();
}
}
catch (RemoteException e){
Toast.makeText(getApplicationContext(), "Error was: " + e,
Toast.LENGTH_LONG).show();
mHelper.flagEndAsync();
mHelper.launchPurchaseFlow(store.this, "remove_ads", 10001,
mPurchaseFinishedListener, "key");
}
}
}
});
and for some reason I get the NPE at the line indicated startIntentSenderForResult... and I don't understand what could cause it. Previously I used this code in another in app purchase exactly as shown except it was a different sku. Could it make a difference since I have 2 identical copies of this code block shown in the same class? Stacktrace doesn't show anything useful either, just the NPE.
I want to share images from my application to Whatsapp but I am not able to do this. I place all images into assets folder. What do I have to change?
public void onClickWhatsApp() {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "Hey, check out this cool game for Android ‘Free Ticket Bollywood Quiz’ www.globussoft.com";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
Your title is about sharing image, but you try sharing text. Well. try any of these.
Try this to share text:
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "This is a test text");
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
Try this to share image:
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
Uri uri=Uri.parse("file:///android_asset/myimage.png");
whatsappIntent.setType("image/*");
whatsappIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
Hope it helps.