How to open an app from another app in android? - android

I have an application named as "App" and another application named as "App1", I have a button in "App" when I click that button I want to open "App1", for this I am using Intent but it does not open "App1".Please help
here is my code for button in "App":-
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app);
init();
}
public void init(){
mTranslucentBtn = (Button) findViewById(R.id.button);
mTranslucentBtn.setAlpha(0.7f);
mTranslucentBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v){//calling an activity using <intent-filter> action name
startNewActivity(MainActivity.this,"com.example.devui1.rewardapp");
}
});
}
public void startNewActivity(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent == null) {
// Bring user to the market or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + packageName));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}

In order for getLaunchIntentForPackage() to work, that package needs to have an Activity that is suitable for being launched as an entry point into that app. The documentation includes this:
The current implementation looks first for a main activity in the category CATEGORY_INFO, and next for a main activity in the category CATEGORY_LAUNCHER. Returns null if neither are found.
This suggests you need one of the following on an Activity in your other package:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.INFO" />
</intent-filter>
Or
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If that's not something you want, then you could try creating your own custom action string and using that to launch an activity exposing that specific action:
<intent-filter>
<action android:name="your.package.ACTION_NAME" />
</intent-filter>
public void startActivityWithPrivateAction(Context context, String packageName) {
Intent intent = new Intent("your.package.ACTION_NAME");
intent.setPackage(packageName);
List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent, 0);
if (activities.isEmpty() {
// no suitable activity was found; open the market instead.
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=" + packageName));
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}

Related

Start Activity from different Application (APK)

I have Two Applications AppOne & AppTwo
App Two has activity as the following:
<activity android:name=".AppTwoActivity">
<intent-filter>
<action android:name="com.myAction.TestAction"></action>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
What I should do to lunch AppTwoActivity from AppOne (Different App)?
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent
Intent intent = new Intent();
startActivity(intent);
}
});
Add the below code to the manifest of your App One. Replace the name with your second apps package name.
<queries>
<package android:name="second.app.package.name" />
</queries>
Then launch that activity using the code below. Or you can use #CommonsWare answer too.
Intent intent = new Intent();
intent.setClassName("second.app.package.name", "second.app.package.name.ActivityName");
startActivity(intent);
Use an Intent that will resolve to the other app's activity:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent
Intent intent = new Intent("com.myAction.TestAction");
try {
startActivity(intent);
} catch (Throwable t) {
Log.e("AppOne", "Exception starting AppTwo", t);
// TODO something to let the user know that the other app is not installed or we otherwise cannot start the activity
}
}
});

How to correctly handle custom URL scheme in my app (avoid handling multiple times)?

I have added a custom URL scheme to my Android 4+ app to be able to deep link to some view/activity:
// AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<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.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="myapp"/>
<data android:host="webservice"/>
</intent-filter>
</activity>
// MainActivity
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
if (uri.getScheme().equalsIgnoreCase("myapp") && uri.getHost().equalsIgnoreCase("webservice")) {
Intent settingsIntent = new Intent(MainActivity.this, WebServiceSettingsActivity.class);
startActivity(settingsIntent);
}
}
}
This works fine, when a link like myapp://webservice/something is used, my app is startet and the WebServiceSettingsActivity is shown.
BUT: When pressing the back button, the WebServiceSettingsActivity is shown again. It seem, that MainActivity is re-startet with the same intent as before and thus the settings are shown again...
This can be repeated indefinitely. How to solve this?
Your MainActivity is opening your WebServiceSettingsActivity, but you're never finishing the former. This leaves you with 2 choices:
Call this.finish(); immediately after calling startActivity(settingsIntent);
This will kill your MainActivity so that there is nothing to return to after finishing the WebServiceSettingsActivity.
2 (better). Open your WebServiceSettingsActivity with startActivityForResult() instead of startActivity(), and finish your MainActivity when, and only when, returning from your WebServiceSettingsActivity:
private static final int REQUEST_CODE_WEB = 1;
public void onCreate(Bundle savedInstance) {
...
if (...) {
startActivityForResult(settingsIntent, REQUEST_CODE_WEB);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_WEB) {
finish(); // Or do whatever you want, such as load your main content
}
}

intent-filter doesn't work with ActivityTestRule in android

I have an application that accepts deeplink.
Manifest.xml:
<activity
android:name=".activities.unsigned.MagicLink"
android:label="Some test">
<intent-filter android:label="Test">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.myapp" />
</intent-filter>
</activity>
<activity
android:name=".activities.unsigned.MainScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Activity:
public class MagicLink extends BusAppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent != null && intent.getAction() != null) {
Uri data = intent.getData();
ServicesApi servicesApi = ServicesApi.init(this);
servicesApi.setSessionId(data.getQueryParameter(HttpRemoteApi.SESSION_ID));
startActivity(new Intent(this, LoginActivity.class));
}
}
}
This thing works perfectly if user use it. Well I want to create a test for it now. So I write something like this:
androidTest:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class LoginTest {
#Rule
public final ActivityTestRule<MainScreen> main = new ActivityTestRule<>(MainScreen.class);
#Test
public void checkSmth() {
clickMagicLink();
//...
}
private void clickMagicLink() {
String url = "com.myapp://login?session_id="+utils.getSessionId();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
main.launchActivity(i);
}
}
But instead of starting MagicLink activity this thing starts MainScreen activity (which is MAIN). What do I do wrong?
P.s. I also saw something like this: new ActivityTestRule<>(MainScreen.class,true, false);. But with this constructor my test start, but android app doesn't (I mean emulator starts but app doesn't)
ActivityTestRule.launchActivity() always starts the activity being tested. You cannot use it to start any other activity. In this case, it will always start MainActivity. The Intent parameter is passed to the activity. This allows you to send extras during a test. The intent is not used to select which activity to launch.
Also note that the docs say
Don't call this method directly, unless you explicitly requested not to lazily launch the Activity manually using the launchActivity flag in ActivityTestRule(Class, boolean, boolean).
If you want to test your MagicLink activity, you can use ActivityTestRule<MagicLink>:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class MagicLinkTest {
#Rule
public final ActivityTestRule<MagicLink> main = new ActivityTestRule<>(MainScreen.class, false, false);
#Test
public void testMagicLink() {
String url = "com.myapp://login?session_id="+utils.getSessionId();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
main.launchActivity(i);
// assertions go here
}
}
You can also use ActivityTestRule<MainScreen> but you have to simulate the exact same actions as an actual user.

How to send data between one application to other application in android?

i've tried to sending data between App1 to App2 via Intent in Android
i used this code but i couldn't resolve my problem.
App1 MainActivity :
Intent i2 = new Intent("com.appstore.MainActivity");
i2.setPackage("com.appstore");//the destination packageName
i2.putExtra("Id", "100");
startActivity(i2);
App2 MainActivity :
Bundle data = getIntent().getExtras;
if(data!=null){
String myString = b.getString("Id");
}
Manfiest App2 MainActivity:
<activity android:name=".MainActivity">
<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="image/*" />
</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>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
Final code:
App 1 :
Intent intent = new Intent();
intent.setClassName("com.appstore", "com.appstore.MyBroadcastReceiver");
intent.setAction("com.appstore.MyBroadcastReceiver");
intent.putExtra("KeyName","code1id");
sendBroadcast(intent);
App 2:
Reciver:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Data Received from External App", Toast.LENGTH_SHORT).show();
}
}
Manifest :
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="first_app_packagename" />
</intent-filter>
</receiver>
MainActivity :
MyBroadcastReceiver mReceiver = new MyBroadcastReceiver();
registerReceiver(mReceiver,
new IntentFilter("first_app_packagename"));
My requirement was to send the "user id" from App1 to App2 and get "username" back to App1.
I needed to launch my app directly without any chooser. I was able to achieve this using implicit intent and startActivityForResult.
App1 > MainActivity.java
private void launchOtherApp() {
Intent sendIntent = new Intent();
//Need to register your intent filter in App2 in manifest file with same action.
sendIntent.setAction("com.example.sender.login"); // <packagename.login>
Bundle bundle = new Bundle();
bundle.putString("user_id", "1111");
sendIntent.putExtra("data", bundle);
sendIntent.setType("text/plain");
if (sendIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(sendIntent, REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getBundleExtra("data");
String username = bundle.getString("user_name");
result.success(username);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
I had two activity in App2 ie. MainActivity and LoginActivity.
App2 > AndroidManifest.xml
<activity android:name=".LoginActivity">
<intent-filter>
<!--The action has to be same as App1-->
<action android:name="com.example.sender.login" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Sorry for this I had a little mix up with Java and Kotlin. My second app was in Kotlin, not that it will effect in any way.
App2 > LoginActivity.java
override fun onResume() {
super.onResume()
var userId = "No data received"
val intent = intent
if (intent != null
&& intent.action != null
&& intent.action.equals("com.example.sender.login")
) {
val bundle = intent.getBundleExtra("data")
if (bundle != null) {
userId = bundle.getString("user_id")
userId = " User id is $userId"
}
}
tvMessage.text = "Data Received: $userId"
}
fun onClickBack(view: View) {
val intent = intent
val bundle = Bundle()
bundle.putString("sesion_id", "2222")
intent.putExtra("data", bundle)
setResult(Activity.RESULT_OK, intent)
finish()
}
When you do this:
Intent i2 = new Intent("com.appstore.MainActivity");
i2.setPackage("com.appstore");//the destination packageName
i2.putExtra("Id", "100");
startActivity(i2);
you are calling the single-argument constructor of Intent. In this constructor, the argument is interpreted as the Intent ACTION. You then set the package name in the Intent.
When you call startActivity() with this Intent, Android will look for an Activity that contains an <intent-filter> with the specified ACTION. There are no installed applications that have an Activity defined like this in the manifest:
<activity>
<intent-filter>
<action android:name="com.appstore.MainActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
So Android will not be able to find and launch the Activity that you want.
As you want to specify explicitly the component that you want to use, instead of using the 1-argument Intent constructor, you should do this instead:
Intent i2 = new Intent();
i2.setClassName("com.appstore", "com.appstore.MainActivity");
i2.putExtra("Id", "100");
startActivity(i2);
Using setClassName() you provide the package name and the class name of the component that you want to launch.
This should work:
APP1
Intent i2 = new Intent();
i2.setComponent(new ComponentName(PACKAGE,ACTIVITY));//the destination packageName
i2.putExtra("Id", "100");
startActivity(i2);
APP2
String myString = getIntent().getStringExtra("Id");
Using Bundle.putSerializable(Key,Object); and Bundle.putParcelable(Key, Object);
the former object must implement Serializable, and the latter object must implement Parcelable.
Content providers:
Content providers are the standard interface that connects data in one process with code running in another process.
See Android Docs.
Content provider working demo here.

Android Skype Intent

i am currently developing a simple app on android. I have an imageview (Skype Intent) with a setonclicklistener. When someone clicks on this imageview appears the dialog "Complete action using...." . The dialog contains the skype option which is the desired one, but also contains all the apps which i have developed on my computer. When someone hits the imageview i want to has the option to complete the action only with skype application. If is possible to disappear the dialog "Complete action using..." at all and open skype automatically it would be better.
The skype Intent:
final Intent sky = new Intent("android.intent.action.VIEW");
sky.setData(Uri.parse("skype:" + ""));
ImageView imSky = (ImageView) findViewById(R.id.imageView2);
imSky.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(sky);
}
});
Android Manifest.xml with the 2 intent filter which i have,
<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.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="skype" />
</intent-filter>
Thank you in advance...
This code will launch Skype, is that what you want?
Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage("com.skype.raider");
if (intent != null)
{
// start the activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else
{
// bring user to the market
// or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id="+"com.skype.raider"));
startActivity(intent);
}

Categories

Resources