I have a service running into my device. When this service execute a specific task, i want to create an intent. This intent should open another application and at the same time, perform a button click that is implemented in this second app. How do I SEND and GET this intent?
This is the manifest of the second application.
<activity
android:name="com.example.app2">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
I don't know how to implement the sender. Is this correct?:
Intent in = new Intent("com.example.app2.MAIN");
context.startService(in);
I solved implemented a simply intent a put Extra:
Intent ii = new Intent();
ii.setComponent(new ComponentName("com.example.app2", "com.example.app2.INTENT_MAIN"));
ii.putExtra("ACTION", "TEST");
this.startService(ii);
and into the second activity, I read the intent extra value:
Intent ii = new Intent();
ii.getStringExtra("ACTION");
Related
I have two android apps(i.e. BIApp and EApp) and I am trying to open EApp from BIApp.
EApp has two activity one is MainActivity and another is LandingPageActivity.
When I try to launch MainActivity using startActivity(intent) it works fine but if I try to launch LandingPageActivity. startActivity(intent) does nothing.
if EApp is open in background then startActivity(intent) works for LandingPageActivity too.
Below is the code snippet that I am using.
Intent intent = new Intent();
intent.setClassName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity");
startActivity(intent);
I want to start LandingPageActivity.
What should I do ?
Try this:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
startActivity(intent);
and add
android:exported="true"
and in the intent-filter add
<category android:name="android.intent.category.DEFAULT"/>
to the declaration of LandingPageActivity in the manifest.
Try this, this is working for me
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
startActivity(intent);
Read how to allow Other Apps to Start Your Activity
You only need to add android.intent.category.DEFAULT category to your Activity onto manifest. Like below
<activity android:name="com.pkg.eapp.LandingPageActivity">
<intent-filter>
<action android:name="com.pkg.eapp.action.OPEN_LANDING_PAGE" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
and call it as
Intent intent = new Intent("com.pkg.eapp.action.OPEN_LANDING_PAGE");
intent.setComponent(new ComponentName("com.pkg.eapp", "com.pkg.eapp.LandingPageActivity"));
// Or intent.setPackage("com.pkg.eapp");
startActivity(intent);
I have never done this but may be LandindPageActivity is not accessible by other apps. In your EApp manifest, do you have an intent filter like this for the LandingPageActivity ?
<intent-filter>
<action android:name="com.pkg.eapp.LandingPageActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Try this:
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.pkg.eapp.LandingPageActivity");
startActivity(LaunchIntent);
Hope this might help you.
I have this Activity defined in my AndroidManifest.xml:
<activity
android:name=".Splash"
android:label="#string/app_name"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
When I am trying to call it in this way:
Intent splashActivity = new Intent("android.intent.action.Splash");
mainAppContext.startActivity(splashActivity);
I get:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.Splash }
And when I try to start it in this way:
Intent splashActivity = new Intent("android.intent.action.MAIN ");
mainAppContext.startActivity(splashActivity);
It open a Complete action using dialog with many options there like play store etc.
Please help in starting this Activity.
From some other Activity:
startActivity(new Intent(this, Splash.class));
Intent splashActivity = new Intent("android.intent.action.Splash");
No-no-no! It should to
Intent splashActivity = new Intent(context, Splash.class);
If you use action - it seem that you want activity for some type of action, for example, view webpage on browser.
In your situation you have to use concrete activity and set it to intent
<action android:name="android.intent.action.MAIN" />
this action is not for your app, it is needed to android's launcher, that find activity in your application to launch it
Official documentation about intent mechanism
Where are you trying to launch this activity from? The following lines imply that Splash is your main Activity:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Are you trying to launch the Splash Activity from Splash itself? This would be problematic. If you are launching from another Activity then you can use the following to launch Splash:
Intent intent = new Intent();
intent.setClass(YourCurrentActivity.this, Splash.class);
startActivity(intent);
Activity not found exception is occurred when u are trying to use or start activity that is not declared into the Mainfeast.xml file.
And When you want to start any activity then you need to start that activity by using intent. but in android "android.intent.action.Splash" Intent Action exist. And you have declared the following intent filter
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
"android.intent.action.MAIN" this is the action name that indicate the this is the activity to be started first. and android send this intent and an activity that is registered with this intent action is the first activity to be started first.
So to start splash activity you doesn't need any intent. You just need to simply run this project. but If u want to Start Another activity from splash actiivty then u need to do following things.
Intent intent = new Intent(Context, ActivityName.class);
startActivity(intent);
or
startActivityForResult(intent, requestCode);
and remember that You have to declare that activity into the mainfeast.xml
I have 2 projects. One is my Main Project(A) , and another is a Library Project(B). I want to start an activity which is present in A from an activity which is located in B. How do I do that ?
I Tried startActivity(getApplicationContext(),B.class);
,but
B.class
is not resolved.
How Can I let my library project start an activity of my main project ?
You shouldn't need to use an intent filter. The code in Activity A can use
ComponentName cn = new ComponentName(this, "my.package.MyActivity.B");
Intent intent = new Intent().setComponent(cn);
startActivity(this, intent);
to specify the activity B should be started.
You can add custom Action in intent-filter of you activity and start that activity by specifying action
<activity android:name="my.package.MyActivity">
<intent-filter>
<action android:name="my.package.action.MY_ACTION"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="my.package"/>
</intent-filter>
</activity>
start activity with this code:
final Intent intent = new Intent("my.package.action.MY_ACTION");
intent.addCategory(getActivity().getPackageName());
startActivity(getActivity(), intent);
I'm writing an app updater for my app. After I make sure I have my apk on the device, this is what I do from within the app I'm trying to update:
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
File f = new File(apkLocation);
promptInstall.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
_context.startActivity(promptInstall);
This launches my installer which displays the app permissions and I am able to click "Install". But from here the app simply closes, I get no message whatsoever (I would've expected the dialog telling me the install was successful giving me the option to press "Close" or "Open"). It just goes to the main screen of the device without further notice.
On a side note, the app is indeed updated when I manually open it back.
How can I make the installer go all the way as expected? Is there anything to set on the intent?
While writing this, I'm wondering if the reason this happens is that the current app is simply overwritten on the device thus closing it and by extent not getting the result of the intent because it's source was killed?
All you can do is register a receiver with the intent filters like android.intent.action.PACKAGE_INSTALL or android.intent.action.PACKAGE_REPLACED from which you can restart your application back again.
<receiver android:enabled="true" android:exported="true" android:label="BootService" android:name="com.project.services.BootService">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_CHANGED"/>
<data android:scheme="package"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
</application>
And
public class BootService extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
Intent serviceIntent = new Intent();
serviceIntent.setClass(context,Controller.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
} else if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
Intent serviceIntent = new Intent();
serviceIntent.setClass(context, Controller.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
}
}
}
To successfully update you need to launch intent with URI indicating to your update app as new task.
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(PATH_TO_APK));
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
My post below:
Android application update issue
First off, you can't install without prompt, unless you are rooted or have system privileges. I don't think you were asking that, but one of your paragraphs isn't clear.
Secondly, if installing an update version of a running app, the behavior you're seeing is expected: The app is force-closed and updated. You can't update in-place. You can detect when the installation was aborted, because the activity invoking the installer will be resumed.
In order to update a running app AND keep it running, you'll need a separate process (app) to monitor the installation and restart your app.
I have two apps, A and B.
I want to call B by A.
My code in A is as below:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.example.bapp","com.example.bapp.BActivity"));
intent.putExtra ("test2abc", "abctest2");
startActivity(intent);
And B's intent filter in manifest as below:
<intent-filter>
<action android:name="ACTION_BACKCALL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
But it will close B before launch B while B had opened.
I find below code to open *.txt file. This will open two txt reader app at the same time.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mimetype);
startActivity(inte
nt);
How can I arrive that?
hey i found this code snippet Launch an application from another application on Android which will work for you
set the address of the application which you want to launch
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
you might want to try android:launchMode="singleInstance" and
android:finishOnTaskLaunch="true" while defining launcher activity.
<activity
android:name="com.example.test.sampleMediaPlayer"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:finishOnTaskLaunch="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
For Activity A.