I am getting Cannot find symbol method startActivityForResult(Intent,int) - java

I want to stream an URL in the MX video player and in VLC. When the URL ends with comx, the MX Video Player handles the work well but when it ends with covlc, it gives me an error. The error is:
cannot find symbol method startActivityForResult(Intent,int)
Here is the class:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String str=new String(url);
if(str.endsWith("comx")){
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Uri videoUri = Uri.parse(url.replace("?comx", ""));
intent.setClassName("com.mxtech.videoplayer.ad","com.mxtech.videoplayer.ad.ActivityScreen");
intent.setDataAndType(videoUri, "application/x-mpegURL");
intent.putExtra("secure_uri", true);
intent.putExtra("title", "Kung Fury");
intent.setPackage("com.mxtech.videoplayer.ad");
view.getContext().startActivity(intent);
//view.goBack();
return true;
}
else {
if(str.endsWith("covlc")){
Uri uri = Uri.parse(url.replace("?covlc", ""));
int vlcRequestCode = 42;
Intent vlcIntent = new Intent(Intent.ACTION_VIEW);
vlcIntent.setComponent(new ComponentName("org.videolan.vlc", "org.videolan.vlc.gui.video.VideoPlayerActivity"));
vlcIntent.setDataAndType(uri, "video/*");
vlcIntent.putExtra("title", "Kung Fury");
vlcIntent.putExtra("from_start", false);
vlcIntent.putExtra("subtitles_location", "/sdcard/Movies/Fifty-Fifty.srt");
view.getContext().startActivityForResult(vlcIntent, vlcRequestCode);
return true;
}
else {
//Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//view.getContext().startActivity(intent);
return false;
}
}
}
}

startActivityForResult() is a method on Activity, not Context. Probably the Context returned by getContext() is an Activity, so you can cast it and call startActivityForResult():
((Activity)view.getContext()).startActivityForResult(vlcIntent, vlcRequestCode);
It would be safer to pass the Activity into the MyAppWebViewClient more directly (e.g., constructor parameter) and use it that way.

Related

Android put extra doesn't send extras to another activity

I try to send some values to another activity.
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
Intent intent = new Intent(GroupsMain.this, AboutGroup.class);
intent.putExtra("groupName", "Hello");
startActivity(intent);
}
#Override
public void onLongClick(View view, int position) {
}
}));
And so on AboutGroup activity I try to get extra.
1 way:
Bundle extras = getIntent().getExtras();
String name = extras.getString("groupName");
and second way:
Intent intent = new Intent();
String name = intent.getStringExtra("groupName");
But nothing works for me. On AboutGroup activity i get empty string. Please help me fix this problem.
try this
Send:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("groupName", "Hello Anna");
startActivity(intent);
get extra:
String name = getIntent().getStringExtra("groupName");
myTextview.setText(name);

App crashes when starting a activity

I have a webview app and wanted to updated app from inside when a certain text is present in the url.
I am calling these in shouldOverrideUrlLoading function of webview:
Intent intent = new Intent(getApplicationContext(), Update.class);
startActivity(intent);
return true;
And here is the Update.class
public class Update extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "app-file.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
File file = new File(destination);
if (file.exists())
file.delete();
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(getIntent().getStringExtra("http://example.com/app-file.apk")));
request.setDestinationUri(uri);
dm.enqueue(request);
final String finalDestination = destination;
final BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(ctxt, BuildConfig.APPLICATION_ID + ".provider", new File(finalDestination));
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openFileIntent.setData(contentUri);
startActivity(openFileIntent);
unregisterReceiver(this);
finish();
} else {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(install);
unregisterReceiver(this);
finish();
}
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}
When I go to the certain url to start the activity, the app crashes. I would love to see any solution. Thanks!!
I SOLVED IT, it was permission error.
I would suggest you to look into ADB Logcat, which will help you to fix the issue.
Otherwise, check into crash log or make sure that you have declared your Update Activity in manifest.xml
Make sure you declare the new activity in your Android Manifest.xml
try changing this
Intent intent = new Intent(getApplicationContext(), Update.class);
startActivity(intent);
return true;
to this
Intent intent = new Intent(getApplicationContext(), Update.class);
startActivity(intent);
finish();
return true;
Let me know how that went.

How can I open play store market:// in webview [duplicate]

I have some links in my webview that are market:// links. When my users tap on them, it gives them a page cannot be found error.
How can I allow all links that begin with market:// to automatically open the Google play store when they are tapped? I tried:
final Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("market://details?id="));
startActivity(intent);
}
but that didn't seem to do anything. I am pretty new to this so any help would be appreciated. Also, FYI, I cannot change the market:// links to play.google.com myself. They are from my advertiser.
Is there anyway I can include it in this code:
public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {
if (DEBUG)
Log.e("shouldOverride", paramString);
if (Uri.parse(paramString).getHost()!=null && (!Uri.parse(paramString).getHost().equals("market.android.com")) && (!paramString.contains("facebook.com")) && (!Uri.parse(paramString).getHost().contains("twitter.com")) && (!Uri.parse(paramString).getHost().equals("play.google.com"))
&& (!Uri.parse(paramString).getHost().contains("bit.ly")) && (!Uri.parse(paramString).getHost().contains("plus.google.com")) && (!Uri.parse(paramString).getHost().contains("youtube.com"))){
if(isAppOrGamePage(paramString)){
final Intent intent = new Intent(MainActivity.this, PageActivity.class);
intent.putExtra("app_url", paramString);
startActivity(intent);
} else
return false;
} else {
final Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(paramString));
startActivity(intent);
}
return true;
}
}
You can decide what to do by looking the scheme of the url, if Google Play Store app is installed you can open the detail page in Play Store app, else you can show Google Play web page of the application
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
// Google Play app is not installed, you may want to open the app store link
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
}
});
you can use this code like this also if its help you:
// It will not work in android simulator as it does not have Google Play Store
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+APP_ID)));
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}

How to implement a "share on whatsapp" feature in a custom Android app?

i have read several already answered article on this site and used this
Sending message through WhatsApp
i am able to share from chrome but not from app.
my code is
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(MainActivity.this, "WhatsApp not Installed", Toast.LENGTH_SHORT).show();
}
}
when i click on share button in my android app whatsapp icon it gives error page not found but when same thing is shared from chrome it just works fine.
my url is http://way2enjoy.com/app/jokes.php
if anyone can guide where the mistake is i will be thankful
you can use my code which works for me
void openWhatsappContact(String number) {
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));}
enjoy your code time:)
you can use this code also
//method used to show IMs
private void show_custom_chooser(String value) {
List<ResolveInfo> list = null;
final Intent email = new Intent(Intent.ACTION_SEND);
email.setData(Uri.parse("sms:"));
email.putExtra(Intent.EXTRA_TEXT, "" + value);
email.setType("text/plain"); // vnd.android-dir/mms-sms
WindowManager.LayoutParams WMLP = dialogCustomChooser.getWindow()
.getAttributes();
WMLP.gravity = Gravity.CENTER;
dialogCustomChooser.getWindow().setAttributes(WMLP);
dialogCustomChooser.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialogCustomChooser.setCanceledOnTouchOutside(true);
dialogCustomChooser.setContentView(R.layout.about_dialog);
dialogCustomChooser.setCancelable(true);
ListView lvOfIms = (ListView) dialogCustomChooser
.findViewById(R.id.listView1);
PackageManager pm = getPackageManager();
List<ResolveInfo> launchables = pm.queryIntentActivities(email, 0);
// ////////////new
list = new ArrayList<ResolveInfo>();
for (int i = 0; i < launchables.size(); i++) {
String string = launchables.get(i).toString();
Log.d("heh", string);
//check only messangers
if (string.contains("whatsapp")) {
list.add(launchables.get(i));
}
}
Collections.sort(list, new ResolveInfo.DisplayNameComparator(pm));
int size = launchables.size();
adapter = new AppAdapter(pm, list, MainActivity.this);
lvOfIms.setAdapter(adapter);
lvOfIms.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
ResolveInfo launchable = adapter.getItem(position);
ActivityInfo activity = launchable.activityInfo;
ComponentName name = new ComponentName(
activity.applicationInfo.packageName, activity.name);
email.addCategory(Intent.CATEGORY_LAUNCHER);
email.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
email.setComponent(name);
startActivity(email);
dialogCustomChooser.dismiss();
}
});
dialogCustomChooser.show();
}

Java, android WebView

I have built a WebView application in java.
Basically what i did was Created a WebView loaded a url, but what happened is when i click on the Uri tel:, the app crashes i have no idea what to do.
I have tried to override the Web. I did override WebViewClient.
App always crashing if uri = tel something by this code:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("html5rocks.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
Try this:
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}
You need ACTION_DIAL and not ACTION_VIEW.

Categories

Resources