App crashes when starting a activity - java

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.

Related

How to play recorded video in another Activity by passing in intent in android studio?

I am recording a video and passing its path into VideoView Activity. But the video can't be opened. Can anyone please help?
RecordActivity - video path function-:
public static String getVideoFilePath() {
return getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "rangeela.mp4";
}
Its returning ->
file:///storage/emulated/0/DCIM/202209_02-231740rangeela.mp4
File mediaFile = new File(getVideoFilePath());
Uri uri= Uri.fromFile(mediaFile);
Intent intent=new Intent(this,VideoEditorActivity.class);
intent.putExtra("uri",uri.toString());
startActivity(intent);
VideoViewActivity
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
Uri uri=Uri.parse(bundle.getString("uri"));
Log.e("video path = ", String.valueOf(uri));
binding.videoView.setVideoURI(uri);
binding.videoView.start();
}
But I am getting an error
Can't play this video
How to pass the recorded video in intent and play in another activity?
Try this
String path = getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "rangeela.mp4";
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this,VideoEditorActivity.class);
intent.putExtra("video",path);
}
});
VideoViewActivity
VideoView view = findViewById(R.id.videoview);
String path = getIntent().getStringExtra("video");
view.setVideoPath(path);
view.start();

Detect when installation has finished in Intent using ACTION_INSTALL_PACKAGE

I have a launcher type application from which user can install different apps. It is working completely fine but the apk's from different apps just keep stacking up and taking too much space.
Is there a way that I can detect that application installation has completed when using new Intent(Intent.ACTION_INSTALL_PACKAGE) and then I could delete the previously downloaded apk... after the installation has completed.
Here is some code:
Intent openDownloadIntent = getOpenDownloadedApkIntent(context, file);
try {
context.startActivity(openDownloadIntent);
} catch (ActivityNotFoundException e) {
// TODO
}
private static Intent getOpenDownloadedApkIntent(Context context, File file) {
// The type of intent to use to open the downloaded apk changed in Android N (7.0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri path = FileProvider.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".utils.DownloadedFileProvider",
file);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(path);
return intent;
} else {
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
return intent;
}
}

Open another app activity, if app is not installed go to play store [duplicate]

This question already has answers here:
Open another application from your own (intent)
(20 answers)
Closed 5 years ago.
i want to open an another app activity from my app, if the app is not installed go to the play store to install the app.
my code is working fine but its only open the app from it's package name, it's not opening activity.
Code:
public void startApplication(String packageName)
{
try
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(intent, 0);
for(ResolveInfo info : resolveInfoList)
if(info.activityInfo.packageName.equalsIgnoreCase(packageName))
{
launchComponent(info.activityInfo.packageName, info.activityInfo.name);
return;
}
// No match, so application is not installed
showInMarket(packageName);
}
catch (Exception e)
{
showInMarket(packageName);
}
}
private void launchComponent(String packageName, String name)
{
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setComponent(new ComponentName(packageName, name));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void showInMarket(String packageName)
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
Try
public void startApplication(String packageName) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if (launchIntent != null) {
startActivity(launchIntent);
} else {
showInMarket(packageName);
}
}

Parsing Error on downloading and running an .apk file

I have a feature in my app which allows the user to download a new version.
But my problem is when I start the intent which should start the installation proccess there is a message:
Parse error. There was a problem parsing the package
This is my code
Button button;
String urlApp = "example.com/app.apk"
String fileName = "app.apk";
DownloadManager downloadManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File(Environment.DIRECTORY_DOWNLOADS, fileName);
if (file.exists()){
boolean deleted = file.delete();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlApp));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("Click to update");
downloadManager.enqueue(request);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
});
}
private void openFile() {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
"application/vnd.android.package-archive");
startActivity(install);
}
BroadcastReceiver onComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
openFile();
}
};
}
It's just for .apk files, .txt files work fine. There is neither a error on the logcat.
The deleting is also not working but this isn't my primary problem.
Thanks in advance :D
Sorry if this is a stupid problem but this is my first time working with the download manager
Ok, i finally solved it
Instead of:
install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
"application/vnd.android.package-archive");
i used
install.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+fileName)),
"application/vnd.android.package-archive");
I have come across the same problem earlier.
Please check the Min API level for your new Application version and the API level of the device you are using.

how to use utility class to start intent android

I am updating code for a company app and there are about 20 activity classes that all download a PDF and then display it using this code:
public void showPdf()
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
the code is working, however it has been replicated in all 20 classes (seems very bad to me) and I would like to put it into a single class which each activity class imports, however when I try to do this, things like getPackageManager() and startActivity(intent) no longer work.
How can I structure my class to make this happen? or am I going about this the wrong way.
How can I structure my class to make this happen?
Step #1: Make this a static method on a utility class.
Step #2: Add Context ctxt as a parameter to the method.
Step #3: For methods like getPackageManager() and startActivity(), which are implemented on Context, call them on the passed-in ctxt parameter.
Step #4: Smack your wrist with a ruler for using string concatenation for creating a file path, and do it the right way.
Step #5: Get rid of the queryIntentActivities() code that you aren't using.
public static void showPdf(Context ctxt)
{
File file = new File(Environment.getExternalStorageDirectory(), "/pdf/Read.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);
}
Step #6: Have the places that are presently calling showPdf() call YourUtilityClass.showPdf(this).
public class PDFUtlity{
public static void showPdf(Context context)
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
PackageManager packageManager = context.getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
((Activity)context).startActivity(intent);
}
}
public class MyActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
///.....
PDFUtlity.showPdf(this);
}
}

Categories

Resources