Java How to launch myapp by opening an image? - java

I'm working on an image editor app and I want to have my app being in the 'Open With ...' list when the user clicks on an image from file manager or ... (Like how 'Photo Editor' app did) :
Click to see the example (Edited)
And the problem is that I don't know how to do such a thing.
I have researched in youtube and I found that it's about the intent filter in the Manifest.xml file.
I already tried adding some <action/> to my intent filter by random but it didn't worked.

Go to the Manifest file and search for your LAUNCHER activity for example SplashActivity
like this
<activity
android:name=".SplashActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
//this below code help you to get image from other apps if shared
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*" />
</intent-filter>
</activity>
And To get that data in your SplashActivity do this :
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
//this will check that you are getting only image type not text
boolean isImage = Intent.ACTION_SEND.equals(action) && "image/*".equals(type);
if(getIntent().getData() != null && isImage){
// here you got the data and you can send this data or uri
// to your image editor activity
Uri imageUri = intent.getData();
}
And you can send that image to the next activity by doing this
you can store the URI as a string
Intent nextIntent = new Intent(this, EditorActivity.java);
nextIntent.putExtra("imageUri", imageUri.toString());
startActivity(nextIntent)
and then just convert the string back to URI like this in your (EditorActivity)
Uri myUri = Uri.parse(getIntent().getStringExtra("imageUri"));

Related

Android FCM working partly while app is killed

I have 2 kinds of notification builders in my MyFirebaseMessagingService.java, one passes string called 'lecture_date' and opens Lectures_graph.class while the other one passes string called 'web_url' and opens webview_base.class when clicked.
What i want to achieve : fully working FCM notifications even when app is killed, notifications must open activity (with extras) once they are clicked.
How far i am : I can receive notifications to open Lectures_graph.class with intent extras, everything loads and works just fine. Even if i receive notifications for webview_base.class activity, nothing happens when i click on notification.
There might be one thing that points to problem - Logcat often tells me this :
E/FirebaseMessaging: Notification pending intent canceled
Im using PHP to send out notifications (that code is 100% correct and working), code below shows represents the idea of JSON sent out for webview_base.class activity. To send out notifications for Lectures_graph.class activity, i just have to swap click_action from "OPEN_ACTIVITY_WEBVIEW" to "OPEN_ACTIVITY_LECTURES" and change the data payload. PHP for webview_base.class activity sends away such data :
$msg = array
(
'body' => 'test body',
'title' => "test title",
'click_action' => 'OPEN_ACTIVITY_WEBVIEW',
'channelId' => "newsfeed",
'vibrate' => 1,
'sound' => 1
);
//sends notifications via topics
$fields = array
(
"data" => [
'web_url' => $_POST['notification_url']
],
'notification' => $msg,
'to' => '/topics/test'
);
This is my whole manifest file from Android studio :
<?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="secret">
<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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
<application
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:theme="#style/AppTheme.CustomTheme"
android:usesCleartextTraffic="true"
tools:targetApi="m">
<activity
android:name=".systems.webview_activity.webview_base"
android:configChanges="orientation|screenSize">
<intent-filter>
<category android:name="android.intent.category.BROWSABLE" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="file" />
<data android:mimeType="\*/\*" />
<data android:pathPattern=".*\\.kdb" />
<data android:host="*" />
//even if this method worked with Lectures_graph activity
//it doesnt work with this one
<action android:name="OPEN_ACTIVITY_WEBVIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".systems.lectures.Lectures_graph"
android:configChanges="keyboardHidden|screenSize"
android:label="#string/title_lectures_graph"
>
<intent-filter>
//this intent filter provides correct response
//to click_action which is provided in my PHP file
<action android:name="OPEN_ACTIVITY_LECTURES" />
<action android:name = "android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SplashScreen"
android:configChanges="keyboardHidden|orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NoInternetConnection"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name="secret.systems.about.about_app"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="#style/FullscreenTheme" />
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden|screenSize">
</activity>
<service
android:name="secret.services.MyFirebaseMessagingService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
MyFirebaseMessagingService.java:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
//get key/value from notification
String lectureDate = remoteMessage.getData().get("lecture_date");
String web_url = remoteMessage.getData().get("web_url");
if(lectureDate != null)sendLecturesNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), lectureDate);
else if(web_url != null) sendNotificationWithURL(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), web_url);
else Log.e(TAG, "Message.notification is empty!");
}
}
#Override
public void onNewToken(#NonNull String token) {
Log.d(TAG, "Refreshed token: " + token);
}
private void sendLecturesNotification(String title,String messageBody, String date) {
Intent intent;
intent = new Intent(this, Lectures_graph.class).putExtra("lecture_date", date).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ((int) System.currentTimeMillis()) /* Request code */, intent,
0);
String channelId = getString(R.string.lectures_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.secret)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(((int) System.currentTimeMillis()), notificationBuilder.build());
}
Log.d(TAG, "Lectures notification built with date:"+date);
}
private void sendNotificationWithURL(String title, String messageBody, String web_url) {
Intent intent;
intent = new Intent(this, webview_base.class).putExtra("web_url", web_url).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, ((int) System.currentTimeMillis()) /* Request code */, intent,
0);
String channelId = getString(R.string.newsfeed_channel_id);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.secret)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(((int) System.currentTimeMillis()), notificationBuilder.build());
}
Log.d(TAG, "Webview notification built with URL:"+web_url);
}
}
a while ago i was getting an error in logcat that said something like "default FCM channel is not defined", but im not quite sure thats the problem.
Even if i searched across the whole web, i would like to see better solutions/suggestions for notification (with its payload) handling when app is killed.
So i fixed my nightmare and here is how :
Since i cant open more than one specific activity from notifications, why could i just open my apps main class and handle extra variable from there ?
Instead of having for each wanted activity
<intent-filter>
<action android:name="OPEN_ACTIVITY_???????" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
i made this :
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden|screenSize">
<intent-filter>
<action android:name="OPEN_APP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I added extra variable to notifications payload (examle : 'activity' => 'webview') and renamed click_action in my PHP code. I handled all payloads in MainActivity.class and called wanted android activity from there. MainActivity code :
Bundle extras = getIntent().getExtras();
if(extras == null) {
Log.e("MainActivity", "No Activities passed in notification extras");
} else {
//get opening activity from notification payload
String activity = extras.getString("activity");
if( activity != null){
switch (activity){
//webview activity
case "webview":
//gets url for webview
String web_url = extras.getString("web_url");
if(web_url != null){
finish();
startActivity(new Intent(this, webview_base.class).putExtra("web_url", web_url));
}
break;
//lectures activity
case "lectures":
//gets lecture date
String lecture_date = extras.getString("lecture_date");
if(lecture_date != null){
finish();
startActivity(new Intent(this, Lectures_graph.class).putExtra("lecture_date", lecture_date));
}
break;
}
}
}
Hope this helps someone, someday..
Cheers.

Issue when starting Jitsi video call activity

I am able to run Jitsi video calling android sdk successfully when I add the main video calling activity as the launcher activity of the application, video is getting connected smooth and no worries . However when I changed to the code to calling the same activity from another activity it throws an activity not found exception .
Here is my manifest file
<activity
android:name=".activity.JitsiVideoCallActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize"
android:label="#string/app_name"
android:resizeableActivity="true"
android:supportsPictureInPicture="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="beta.hipchat.me"
android:scheme="https" />
<data
android:host="beta.meet.jit.si"
android:scheme="https" />
<data
android:host="chaos.hipchat.me"
android:scheme="https" />
<data
android:host="enso.me"
android:scheme="https" />
<data
android:host="hipchat.me"
android:scheme="https" />
<data
android:host="meet.jit.si"
android:scheme="https" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="org.jitsi.meet" />
</intent-filter>
</activity>
This is my activity which is supposed to do the video call
public class JitsiVideoCallActivity extends AppCompatActivity {
private JitsiMeetView view;
private static final String ADD_PEOPLE_CONTROLLER_QUERY = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = new JitsiMeetView(this);
Bundle config = new Bundle();
config.putBoolean("startWithAudioMuted", false);
config.putBoolean("startWithVideoMuted", false);
Bundle urlObject = new Bundle();
urlObject.putBundle("config", config);
urlObject.putString("url", "https://meet.jit.si/wizcounsel");
view.loadURLObject(urlObject);
setContentView(view);
}
This is how I launch the intent
#OnClick(R.id.call)
void call() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
askForAudioPermission();
} else
startActivity(new Intent(this, JitsiMeetActivity.class),);
}
I have added JAVA 8 compatibility in my app level gradle file and dependencies on both the gradle files
What I have tried
changing launch mode to singletask App crashes
Making the video call activity the launcher App works
Extend AppCombactActivity and / or JitsiMee Activity App crashes
This is my crash log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.star.star*, PID: 26197
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.star.star/org.jitsi.meet.sdk.JitsiMeetActivity}; have you declared this activity in your AndroidManifest.xml?
If any more info is needed , let me know, thanks in advance, Kindly help
initialize it in oncreate method.In any activity.
void connectCall() {
URL serverURL;
try {
serverURL = new URL("https://meet.jit.si");
} catch (MalformedURLException e) {
e.printStackTrace();
throw new RuntimeException("Invalid server URL!");
}
JitsiMeetConferenceOptions defaultOptions
= new JitsiMeetConferenceOptions.Builder()
.setServerURL(serverURL)
.build();
JitsiMeet.setDefaultConferenceOptions(defaultOptions);
}
now call this method where you want. Example onclick listener
public void onVideoCall(String text) {
if (text.length() > 0) {
// Build options object for joining the conference. The SDK will merge the default
// one we set earlier and this one when joining.
JitsiMeetConferenceOptions options
= new JitsiMeetConferenceOptions.Builder()
.setRoom(text)
.setWelcomePageEnabled(false)
.build();
// Launch the new activity with the given options. The launch() method takes care
// of creating the required Intent and passing the options.
JitsiMeetActivity.launch(activity, options);
}
I think you forgot to register your activity in AndroidManifest.xml. You should register your activity in <application> tag as below.
<activity
android:name=".YourActivity"/>

Unable to start receiver (Activity Not Found)

I developed an application and put that on app store. Afterwords i wanted to change the package name so i just changed the application ID in build.gradle so that it looks appropriate in link. I didn't change anything else not the package name, not the manifest file etc. Application worked fine but now it's showing an error of ActivityNotFound exception on the launcher activity which is called through a broadcast receiver although that activity is defined in manifest file. May i know where am i wrong at?
This is the manifest file coding:
<receiver android:name=".PowerConnectionReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
</intent-filter>
</receiver>
<activity
android:name=".BatteryChargerFast"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And below is the coding of broadcast Receiver:
public class PowerConnectionReceiver extends BroadcastReceiver {
private String TAG="PowerConnectionReceiver";
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClassName("packagename",
"packagename.BatteryChargerFast");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("fast", true);
context.startActivity(i);
}
}
the error states:
java.lang.RuntimeException: Unable to start receiver
packagename.PowerConnectionReceiver:
android.content.ActivityNotFoundException: Unable to find explicit
activity class {packagename/packagename.BatteryChargerFast}; have you
declared this activity in your AndroidManifest.xml?
When you change applicationId in gradle, it overrides the Manifest's id.
so you need to change your code from:
Intent i = new Intent();
i.setClassName("packagename",
"packagename.BatteryChargerFast");
To:
Intent i = new Intent();
i.setClassName("your.new.app.id",
"packagename.BatteryChargerFast");
or may be even simpler where you don't need to consider all this:
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context.getApplicationContext(), BatteryChargerFast.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("fast", true);
context.startActivity(i);
}

Get data from another application in android

I have two application app1 and app2 installed.
And i want to launch an activity of app2 from app1. And then come back to the activity on app1 with some data from app2. But i am able to return back to app1 , but not able to get the data.
from app1 :
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, sampleText);
intent.setType("text/plain");
intent.setComponent(new ComponentName("com.example.app2","com.example.app2.MainActivity"));
startActivity(intent);
from app2:
Intent goBack = new Intent();
goBack.putExtra("result","asdaf");
setResult(RESULT_OK, goBack);
finish();
manifest of app1:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
manifest of app2:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
I am not getting a hit on function in app1.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
TextView tv = (TextView)findViewById(R.id.textView3);
tv.setText(result);
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
What am i missing here?. Is there any settings need to be done for this?
You should be using startActivityForResult in "app1":
startActivityForResult(intent, SOME_CONSTANT);
The constant you pass will be passed back as the requestCode argument. So in your case it looks like you would want to pass in 1.
See the docs for more info: Android Developers

BroadcastReceiver not receiving download complete action

I am trying to capture download complete events, but my BroadcastReceiver is not receiving them. Here is the receiver:
public class DownloadListenerService extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
System.out.println("got here");
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = settings.edit();
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
String downloadPath = intent.getStringExtra(DownloadManager.COLUMN_URI);
editor.putString("downloadPath", downloadPath);
editor.commit();
}
}
}
Here is the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name="com.example.alreadydownloaded.DownloadListenerService"
android:exported="true">
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
</application>
Anyone see what's wrong?
Use full package name for you receiver like com.example.DownloadListenerService
Add android:exported="true" BroadcastReceiver can receive messages from sources outside its application.
Change the name of the Action in the intent-filter to android.intent.action.DOWNLOAD_COMPLETE
<receiver
android:name="com.example.DownloadListenerService"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.INTERNET" />
The receiver only will be triggered if was registered from your application using registerReceiver(#Nullable BroadcastReceiver receiver,IntentFilter filter);
Code to enqueue Download :
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.google.com/images/srpr/logo4w.png"));
dm.enqueue(request);
I think the action name in your XML is wrong. The docs state that the correct one is: android.intent.action.DOWNLOAD_COMPLETE not DownloadManager.ACTION_DOWNLOAD_COMPLETE - you need to use the constant, not the Java form.
<receiver android:name=".DownloadListenerService" >
<intent-filter>
<action android:enabled="true" android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
<receiver
android:name=".BroadCast_Service.Download_BroadCast"
android:exported="true">
<intent-filter android:priority="1099">
<action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
</intent-filter>
</receiver>
I think you are calling DownloadManger service from the IntentService/Service. If so remove it from there and put it into activity.
add permission in android manifest
<uses-permission android:name="android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS" />
If the download is based on your app then you need to send a broadcast?
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
sendBroadcast(i);

Categories