How to solve "No activity found to handle intent" exception? - java

I'm writing an Android FTP server program. I need to select files to send to client, so I need a file chooser and I wrote this code:
Java:
Intent filechooser= new Intent(Intent.ACTION_GET_CONTENT);
filechooser.addCategory("*/*");
filechooser.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(filechooser, 10);
XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sender">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Chooser"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I get a No activity found to handle intent exception.

You are using wrong file category(/). It should be a type instead of category.
If you want to choose any file from storage you need code like below.
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
Source: here

Related

how to open a map activity from home page android studio java

I want to have a secondary activity which will be a map activity that can be opened by a button click on the main menu of an app. So I am using the built in map activity in android studio and when I try and launch that alone the map works. But when I try to add a main home page with button and then use that button to launch the map it doesn't work and my app closes. Does anyone know the correct way to launch a normal map from a button. I have all the implementations in the gradle files correct. I will include my manifest and main activity below.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_fragment1,container,false);
bt = v.findViewById(R.id.launchmap);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), MapsActivity.class);
startActivity(intent);
}
});
return v;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package = "com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="package_name.permission.MAPS_RECEIVE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MyApplication"
tools:targetApi="31" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MapsActivity"
android:exported="true">
<intent-filter>
<category android:name="android.intent.category.default.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="come.google.android.geo.API_KEY"
android:value="AIzaSyBgSEI9Xvh3TicLaNj8FigA4iRyP1jdEVs" />
<activity
android:name=".SecondActivity"
android:exported="true" >
</activity>
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Follow these steps
https://developers.google.com/maps/documentation/android-sdk/start
#Android_map

OnActivityResult always returns RESULT CANCELED (Code: 0) on Android 11

Sadly, Android 11 storage restrictions are too hard and confusing.
I'm trying to pick a document using the below code
fun onFile(activity: AppCompatActivity?, fragmentContext: Fragment?, isMultiple: Boolean?, pFileMimeTypes: Array<String>?): Intent {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
action = Intent.ACTION_OPEN_DOCUMENT
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, isMultiple ?: false)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
}
val fileMimeTypes = pFileMimeTypes ?: FileUtil.mimeTypes
intent.type = if (fileMimeTypes.size == 1) fileMimeTypes[0] else "*/*"
if (!fileMimeTypes.isNullOrEmpty()) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, fileMimeTypes)
}
val list = activity?.packageManager?.queryIntentActivities(intent, PackageManager.MATCH_ALL)
if (list?.size!! > 0) {
if (fragmentContext == null) {
activity.startActivityForResult(
Intent.createChooser(
intent,
activity.getString(R.string.m_selectFile_txt)
), FILE_CODE
)
} else {
fragmentContext.startActivityForResult(
Intent.createChooser(
intent,
activity.getString(R.string.m_selectFile_txt)
), FILE_CODE
)
}
}
// returning intent is not being used anywhere else
return intent
}
Although I have all files access permission Environment.isExternalStorageManager() but still after selecting document(PDF) from the external storage (download or any folder), OnActvityResult is being called with result code 0. I need the Uri of the selected document to proceed further.
For any other reference, below is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="*****Hidden*******">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_STORAGE_PERMISSION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:exported="true"
android:name=".Main2Activity"
android:launchMode="singleTask"
android:label="#string/title_activity_main2"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.attachmentmanager"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider"
tools:replace="android:resource" />
</provider>
</application>
<queries>
<intent>
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.intent.action.PICK" />
<!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
<data android:mimeType="*/*" />
</intent>
</queries>
</manifest>
I must be missing some point. Everything is working fine below Android 11
Thanks in Advance.
startActivityForResult is deprecated
Refer this link
Use Activity Results API
Official Docs

File Could not be Attached; Creating and sending a PDF with Activity Intent

I've asked a similar question to this before, however, I have done some more research and I am still struggling to figure out what my problem is. Essentially, what I am trying to do is create a form of receipt in a PDF format after a customer has completed their purchase. This newly created PDF needs to then be sent to a separate emailing client (such as Gmail) through an Intent. Compared to my previous question I have now implemented a FileProvider, however I keep getting two different errors. The first one is:
java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content...requires the provider be exported, or grantUriPermission()
This causes the file to not be attached to the email at all. And the second error I can sometimes get is, that it will place the actual URI path into the Recipients section of the email, which you can see below (also causing the file to not be attached)
I have tried quite a few different solutions upon doing my research, such using setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) or addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION), which can be seen here
I've also seen that some posts that said to use android:requestLegacyExternalStorage="true" in the manifest, however, this will then limit my minimum APK level quite a lot.
I even tried what this post suggests, whereby you manually go through and give permissions to all the packages, which still didn't solve my problem.
I've also tried using the grantUriPermission() method, which still didn't work either.
Below, you will see that I have placed some code:
Manifext.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fabricanddecor">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:largeHeap="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".LoginActivity"
android:configChanges="orientation|screenSize"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="adjustPan">
</activity>
<provider
android:authorities="com.example.fabricanddecor"
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
</application>
</manifest>
provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external_files"
path="."/>
</paths>
Method that creates the email and fires Intent (EDIT)
try {
String directory = Environment.getExternalStorageDirectory().getPath();
File file = new File(directory);
if (file.mkdirs()) {
String filePath = directory + "/sale.pdf";
File file2 = new File(filePath);
Uri contentUri = FileProvider.getUriForFile(getContext(), "com.example.fabricanddecor", file2);
Intent intent = new Intent(Intent.ACTION_SEND);
getContext().grantUriPermission("com.example.fabricanddecor", contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_EMAIL, recipient);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_STREAM, contentUri);
intent.setDataAndType(contentUri, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(intent, "Choose an Email Service"));
}
} catch (Exception e) {
e.printStackTrace();
}
Any help or suggestion will be highly appreciated, and thank you for your time. If any further code is required, I will be happy to oblige.

activity is creates twice for no apparent reason

I am writing an application running on my custom device (marshmallow based). the general idea is to prompt a user dialog to enter some information at boot and send it to my server. once the information is successfully sent I update the shared preferences that the information was sent and the dialog will not be prompt anymore.
as always I check to see if the needed permissions are present (read phone state for IMEI and sim info), if not I create an activity that requests permissions and on result starts the user dialog activity. if permissions already exist I create the dialog activity straight from the boot receiver.
however, for some reason, sometimes the permissions activity is created twice (I made sure the boot receiver is only triggered once). this causes as expected the user dialog to be shown twice for no reason. does anyone know how can I solve this? thank you.
here is a code snippet for the boot receiver (the only place where the permissions activity is started).
public class BootReceiver extends BroadcastReceiver {
SharedPreferences mPrefs;
boolean wasUpdated;
#Override
public void onReceive(Context context, Intent intent) {
Log.v("car_num", "got boot receiver");
mPrefs = context.getSharedPreferences("car_num", MODE_PRIVATE);
wasUpdated = mPrefs.getBoolean("is_num_updated", false);
if (!wasUpdated) {
Log.v("car_num", "first time - asking for car num");
if (UtilityFunctions.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
Log.v("car_num", "we have permission lets ask for car num");
Intent i = new Intent();
i.setClassName("com.mgroup.carnumberapp", "com.mgroup.carnumberapp.MainActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.v("car_num", "main activity started");
} else {
Log.v("car_num", "no permission - so requesting");
Intent i = new Intent();
i.setClassName("com.mgroup.carnumberapp", "com.mgroup.carnumberapp.PermissionActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("from", "boot receiver");
context.startActivity(i);
}
} else {
Log.v("car_num", "car number already updated - doing nothing");
}
}
}
edit:
I tried adding
android:launchMode = "singleTop"
however, it happened again.
here is my manifest :
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/MGServiceTheme">
<service
android:name=".SendDetailsService"
android:enabled="true"
android:exported="true"></service>
<activity android:name=".PermissionActivity"
android:launchMode = "singleTop"/>
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action
android:name="android.intent.action.MAIN"
android:theme="#style/Theme.AppCompat.NoActionBar" />
</intent-filter>
</activity>
</application>
In your manifest do android:launchMode="singleTop" for an activity
like
<activity android:name=".MapActivity"
android:launchMode="singleTop"/>
<activity android:name=".MainActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

OneSignal Android not launching app with custom onNotificationOpened handler

I am attempting to migrate an app from Parse to OneSignal. Things have been running fairly smooth except for one issue. If the app has not already been started, clicking (opening) the notification does not launch the app and bring it to the foreground.
I am using a custom onNotificationOpenedHandler:
Main Activity, onCreate
// Init OneSignal
OneSignal.startInit(this).setNotificationOpenedHandler(new NotificationOpenHandler()).init();
NotificationOpenedHandler, also in the launcher Main Activity:
class NotificationOpenHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
#Override
public void notificationOpened(OSNotificationOpenResult result) {
JSONObject data = result.notification.payload.additionalData;
String stationName = data.optString("stationName");
String timestamp = data.optString("timestamp");
String filename = data.optString("filename");
String url = getString(R.string.callResourceUrl) + filename;
Log.d("APP", "Notification clicked");
Intent intent = new Intent(getApplicationContext(), CallActivity.class);
intent.putExtra("stationName", stationName);
intent.putExtra("time", timestamp);
intent.putExtra("url", url);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Here is my Android Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.opheliadesign.tevfd">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:supportsRtl="true"
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:logo="#drawable/ic_launcher"
android:launchMode="singleInstance"
android:theme="#style/Theme.Tevfd">
<activity android:name="com.example.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CallActivity"
android:label="#string/title_activity_call"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
</activity>
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
</application>
</manifest>
Any ideas?
UPDATE
Previously, using Parse, I utilized a BroadcastReceiver with a PendingIntent. It seems a bit unclear how to accomplish this with OneSignal as so much seems to be configured automatically.
If I remove the custom Intent, the application comes to the foreground.
Any help would be greatly appreciated.

Categories

Resources