Open Activity from another application - android

I have been trying to open an Activity from a different app. But is doesn't work. Toast message appears Absent as u can see from code. The Activity I want to open is from package com.example.samarth.upbuttondemo.ActivityA. This Activity has an intent filter. The XML file for the activity I want to open is
android:parentActivityName="com.example.samarth.upbuttondemo.MainActivity">
<intent-filter>
<action
android:name="com.example.samarth.upbuttondemo.ActivityA" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent();
i.setComponent(new ComponentName("com.example.samarth",
"com.example.samarth.ActivityA"));
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(i,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafe = activities.size() > 0;
if(isIntentSafe==true)
{
Toast.makeText(MainActivity.this,"Present",Toast.LENGTH_SHORT).show();
startActivity(i);
}
else
{
Toast.makeText(MainActivity.this,"Absent",Toast.LENGTH_SHORT).show();
}
}
});
}
}

Write this to open whatsapp for example:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
startActivity(launchIntent);
You only have to pass the package of your app as parameter of the method getLaunchIntentForPackage("com.yourpackage")

Lets say you want to navigate from activiry x from App A to activity y from App B.
so add this in activity x (App A):
//calling an activity using <intent-filter> action name
Intent inent = new Intent("com.hmkcode.android.another.app.ANOTHER_ACTIVITY");
startActivity(inent);
Now add this intent filter in the manifest of activity y (App B)
<intent-filter>
<action android:name="com.hmkcode.android.another.app.ANOTHER_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
More details and explanation here in this link

Related

Broadcast Reciever not working in Android

Activities:
MainActivity (Splash Screen)
MainActivity2 (Login)
SignupActivity (Signup)
MainActivity3 (Registering details)
ChatActivity (Hide on quiting from here)
Basically what i am trying to accomplish i i wanna hide my app after i do the initial signup part which will be handled uptill MainActivity3 and then when the user quits from the app the app icon should disappear and must only appear when called from dialer. My BroadcastReciever class never gets triggered,I am unable to figure out where i am going wrong Thanks in advance.
BroadCastReceiver.class
package com.insignia.socialmediasim;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
public class MyBroadCastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String ourCode = "**1234";
String dialedNumber = getResultData();
Log.d("triggered", "onReceive: "+dialedNumber);
if ((dialedNumber.equals(ourCode))){
// My app will bring up, so cancel the dialer broadcast
setResultData(null);
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, com.insignia.socialmediasim.MainActivity.class);
packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
//Intent to launch MainActivity
Intent intent_to_mainActivity = new Intent(context, MainActivity.class);
context.startActivity(intent_to_mainActivity);
}
}}
ChatActivity.class, "This is the class in which i hide my app"
package com.insignia.socialmediasim;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class ChatActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_chat);
Intent intent = getIntent();
Toast.makeText(ChatActivity.this, "Welcome Back to the adobe " + intent.getStringExtra("type"), Toast.LENGTH_LONG).show();
}
#Override
protected void onStop() {
super.onStop();
PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(ChatActivity.this, com.insignia.socialmediasim.MainActivity.class);
packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}}
AndroidManifest.XML
<?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.insignia.socialmediasim">
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
android:allowBackup="true"
android:icon="#mipmap/asd"
android:label="#string/appnameda"
android:roundIcon="#mipmap/asd"
android:supportsRtl="true"
android:theme="#style/Theme.Design.NoActionBar">
<activity android:name=".ChatActivity" />
<activity android:name=".MainActivity3" />
<activity android:name=".SignupActivity" />
<activity android:name=".MainActivity2" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadCastReciever">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
It is recommended that register and unregister the broadcast programmatically in you activity.Try It.
this is a sample code for registering a broadcastReceiver:
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(br, filter);
You can check more information from this document: broadcasts

Why won't my android application open/start?

When someone installs my app from the app store, the Open button (usually found next to the uninstall button upon installation completion) is grayed out, and the application cannot be found in their app drawer.
Also, when I execute from Eclipse choosing Default Activity, it says in the console that it installed the APK, but it does not start. If I select the Splash activity as the default activity in the run configuration, it runs without a hitch, but I still cannot find it in my app drawer afterwards. Help would be appreciated c:
Manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.meteorfiber.gangnamstyle.Splash"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.meteorfiber.gangnamstyle.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.meteorfiber.gangnamstyle.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.meteorfiber.gangnamstyle.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Splash:
package com.meteorfiber.gangnamstyle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
boolean isSplashEnabled = sp.getBoolean("isSplashEnabled", true);
if (isSplashEnabled) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Splash.this.finish();
Intent mainIntent = new Intent(Splash.this,
MainActivity.class);
Splash.this.startActivity(mainIntent);
}
}, SPLASH_DISPLAY_LENGTH);
} else {
finish();
Intent mainIntent = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mainIntent);
}
}
}
MainActivity:
package com.meteorfiber.gangnamstyle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
MediaPlayer ourSong;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
ourSong = MediaPlayer.create(MainActivity.this, R.raw.song);
ourSong.start();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// preventing default implementation previous to
// android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
}
}
Change your intent filter for your starting activity to
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The android launcher along with the settings activity try to find this intent-filter in your application in order to launch the activity. If it can't be found, your activity cannot be launched with a typical launcher and won't be displayed.

Using intents to switch activities

I am reading a android book for beginners, and while following on of the chapters I ran into a problem. The chapter is teaching about intents. I right now I have 2 layouts: main.xml, and digital_clock.xml. And in the AndroidManifest I have these lines of code:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".Chapter11Activity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DigitalClockActivity" >
</activity>
</application>
Also I have the two Activity classes that correspond to the layouts:
Chapter11Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Chapter11Activity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button a2 = (Button)findViewById(R.id.button1);
a2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), DigitalClockActivity.class);
startActivityForResult(myIntent, 0);
}
});
}
}
DigitalClockActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class DigitalClockActivity extends Activity {
public void OnCreate(Bundle sIS) {
super.onCreate(sIS);
setContentView(R.layout.digital_clock);
Button a1 = (Button) findViewById(R.id.button01);
a1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent replyIntent = new Intent();
setResult(RESULT_OK, replyIntent);
finish();
}
});
}
}
When I run the app on my phone and switch to the second activity it shows nothing at all. Am I defining something wrong in the AndroidManifest? The application seems very straight forward, but it is not working. I have check over to make sure I didn't type anything wrong. Is it because I am running a android 2.3.3 phone and using the 1.5 sdk, and something is not backwards compatible? Any answers are appreciated!
~Andrew
Method name in second activity should be onCreate not OnCreate.
To prevent typos like this in the future use #Override on methods that you are overriding:
#Override
public void OnCreate(Bundle sIS) {
// code here
}
Then if you make a typo, the compiler will tell about it.

getting exception when starting new activity on an android

i have read a lot about this but didnt come up with a solution.
this is my first android project.
i am trying yo open an activity from my main activity.
i have doubled check the names and the configuration files, but the startactivity() procedure fails.
debbuger shows runtime errror.
as u will c, the startactivity is called upon onclick event.
i am really stuck here dont know what else to do.
thanks a lot!
* this is the main activity **
package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HelloworldActivity extends Activity {
/** Called when the activity is first created. */
private OnClickListener gtScoringListener = new OnClickListener() {
public void onClick(View v) {
Intent mintent = new Intent(HelloworldActivity.this ,imgScoring.class);
startActivity(mintent);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(gtScoringListener);
}
}
* this is the target activity: *
package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class imgScoring extends Activity {
private OnClickListener backBtnListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(imgScoring.this, HelloworldActivity.class);
startActivity(intent);
}
};
private OnClickListener scoreBtnListener = new OnClickListener() {
public void onClick(View v){
TextView tv = (TextView)findViewById(R.id.editText1);
tv.setText(((Button)v).getText());
}
};
/** Called when the activity is first created. */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button01);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button03);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button02);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.Button04);
button.setOnClickListener(scoreBtnListener);
button = (Button)findViewById(R.id.button3);
button.setOnClickListener(backBtnListener);
setContentView(R.layout.imgscoring);
}
}
this is the androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloandroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="12" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".imgScoring"></activity>
<activity android:name=".HelloworldActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You should've posted the stacktrace, but I assume the error is on this line in your target activity:
button.setOnClickListener(scoreBtnListener);
This will give you a NULL:
Button button = (Button)findViewById(R.id.button1);
because you haven't set a contentview in THAT activity, so there is nothing for findViewById() to return. You'll get a nullpointer when calling that setOnclickLIstener()
Try change class name from imgScoring to ImgScoring everywhere. Java is not allowing class name to start from small letter ;-)
And place setContentView(R.layout.imgscoring); just after your super.onCreate();

JAVA - Android error: The application has stopped unexpectedly please try again

I made an app which is just working fine. But when i click on "cancel" or "ok" button when i run the program, instead of linking me to result.java, the simulator give me "The application has stopped unexpectedly please try again" Here's the code
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.database.CursorJoiner.Result;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;
public class lib_main extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onAddClick (View botton){
AutoCompleteTextView title = (AutoCompleteTextView)findViewById(R.id.Title);
AutoCompleteTextView author = (AutoCompleteTextView)findViewById(R.id.Author);
AutoCompleteTextView isbn = (AutoCompleteTextView)findViewById(R.id.ISBN);
Intent intent = new Intent();
intent.setClass(this,Result.class);
intent.putExtra("Title", title.getText().toString());
intent.putExtra("Author", author.getText().toString());
intent.putExtra("ISBN", isbn.getText().toString());
startActivity(intent);
}
public void onCancelClick(View botton){
Intent intent = new Intent();
intent.setComponent(new ComponentName(this,Result.class));
intent.putExtra("Cancel","Cancel");
startActivity(intent);
}
}
this is Result.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Result extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
TextView resultText=(TextView)findViewById(R.id.resultText);
Bundle bundle =getIntent().getExtras();
if(bundle.getString("Cancel")!= null)
{resultText.setText("operation cancel");}
else
{ String book =bundle.getString ("Title");
String title =bundle.getString ("Author");
String isbn =bundle.getString ("ISBN");
resultText.setText(
getString (R.string.resultOk)+""+book+""+ title+""+isbn);
}
}
}
the error log write "No command output when running:'am start-n com.Library.doc/com.Library.doc.lib_main-a android.intent.action.MAIN-c android.intent.category.LAUNCHER' in device emulator-5554"
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Library.doc"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".lib_main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Result"
android:label="#string/result">
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
Can anyone help me with this problem?
Quick question, have you registered both Activities? If you only provide for one in your manifest file you're going to get this error every time you attempt to do something involving the unregistered activity.

Categories

Resources