How do I launch a specific (explicit) activity of another app - java

I want to launch a specific activity of another app from my app. For example, on the onCreate of my app, I want to launch the activity named Rolling (not main) activity of com.pas.webcam.pro. I have heard that you must have control of both apps to do this because you must add an intent filter to the manifest of the second app. This is not true though, because activity launcher apps in the Google Play Store can launch the Rolling Activity of IP Webcam Pro.
The Activity Launcher app is open source, so I tried reviewing the source code here. It was too complicated though, so I could not figure out how this app magically launches this activity. There are many other questions like this on Stack Overflow, and I have read every one. I have also tried lots of the code from the answers too, like this:
Intent intent = new Intent(); intent.setComponent(new ComponentName("com.pas.webcam", "com.pas.webcam.RollingActivity")); startActivity(intent);
I have also tried variants of this code from other posts. My app always crashes and I get variants (depending on the code I use) of the following error:
An error occurred
Invalid intent operation. Unable to find explicit activity class {com.pas.webcam.pro/com.pas.webcam.pro.Rolling}; have you declared this activity in your AndroidManifest.xml?
I have put both of the following in my Android Manifest and the same thing happens:
<uses-permission android:name="android.permission.GET_INSTALLED_PACKAGES" />
<activity android:name="com.pas.webcam.pro.RollingActivity"
Thanks in advance for any answers, I really appreciate it, as I have been working on this problem for a while.
Edit: Here is the activity of the app I want to launch: https://i.stack.imgur.com/Fa7Xq.jpg
Edit: David Wasser helped me solve the problem by giving me the code neccessary to solve the problem. It actually works! To anyone who wants to launch a specific activity of another app with code, please use this:
Intent intent = new Intent(); intent.setClassName("com.pas.webcam.pro", "com.pas.webcam.Rolling"); startActivity(intent);
You may replace com.pas.webcam.pro and Rolling with the app and activity of your choice, but this method truly works. Problem Solved!😀

Try this:
Intent intent = new Intent();
intent.setClassName("com.pas.webcam.pro", "com.pas.webcam.Rolling");
startActivity(intent);
Since you refer to the app as "IP webcam pro", I'm assuming the package name is "com.pas.webcam.pro" (found by Internet research).

Related

Programmatically open 'About device' page in Settings correctly?

startActivity(new Intent(android.provider.Settings.ACTION_DEVICE_INFO_SETTINGS)); opens this page in Samsung devices:
However, I want it to open this page like the rest of Android devices, how can I do it?
As Pedro Antonio said:
If it doesn't work for SAMSUNG devices I'm afraid it will probably not be possible. At least with no official answer. Many times vendors modify stock Android so for SAMSUNG devices settings app is different than stock AOSP, and the official method will not work.
When we call:
Intent i = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);
startActivity(i);
we are referring to activity com.android.settings.Settings$DeviceInfoSettingsActivity and in logs we can see that activity is started
2020-06-26 11:33:43.804 4838-5126/? I/ActivityManager: START u0 {act=null typ=null flg=0x8000 cmp=ComponentInfo{com.android.settings/com.android.settings.Settings$DeviceInfoSettingsActivity}} from uid 1000
On devices like Huawei P10, Software Information data is part of DeviceInfoSettingsActivity. After some digging i found out that on Samsung S7 device Software Information is Fragment that is called inside DeviceInfoSettingsActivity
2020-06-26 11:44:25.780 7103-9703/? D/Settings: packageName : com.android.settings className : com.android.settings.SubSettings
2020-06-26 11:44:25.812 7103-7103/? D/SubSettings: Launching fragment com.samsung.android.settings.deviceinfo.SoftwareInfoSettings
I am not sure is it possible to access DeviceInfoSettingsActivity code (I couldn't) and see if you can send some extra data to open specific Fragment. So at this point i do not believe that it is possible to launch that specific fragment from intent.
The main point is it seems impossible. Let's look at what is the reason.
The DeviceInfoSettingsActivity can be opened by an intent call like:
Intent intent = new Intent(android.provider.Settings.ACTION_DEVICE_INFO_SETTINGS);
startActivity(intent);
or
Intent intent = new Intent();
intent.setClassName(
"com.android.settings",
"com.android.settings.Settings$DeviceInfoSettingsActivity"
);
startActivity(intent)
As you can see here:
https://android.googlesource.com/platform/packages/apps/Settings/+/master/src/com/android/settings/Settings.java
Settings and all of its inner classes are children of SettingsActivity. By taking a look at source code of SettingsActivity, we found out it is possible to show a sub-settings, passing the fragment name as an intent extra with key ":settings:show_fragment" to the SettingsActivity:
SettingsActivity#onCreate() then SettingsActivity#launchSettingFragment()
If we dig into logs where the target screen is shown, we'd see that the target fragment name is com.samsung.android.settings.deviceinfo.SoftwareInfoSettings.
But the problem is that there is a check on fragment names at SettingsActivity#isValidFragment() which allows specific fragments to navigate to and they are SettingsGateway#ENTRY_FRAGMENTS:
protected boolean isValidFragment(String fragmentName) {
// Almost all fragments are wrapped in this,
// except for a few that have their own activities.
for (int i = 0; i < SettingsGateway.ENTRY_FRAGMENTS.length; i++) {
if (SettingsGateway.ENTRY_FRAGMENTS[i].equals(fragmentName)) return true;
}
return false;
}
The alternative for showing other fragments in settings is to open SubSettings activity which overrides the isValidFragment to accepts every fragment.
#Override
protected boolean isValidFragment(String fragmentName) {
Log.d("SubSettings", "Launching fragment " + fragmentName);
return true;
}
That is exactly what happens when the SoftwareInfoSettings is shown:
D/Settings: packageName : com.android.settings className : com.android.settings.SubSettings
D/SubSettings: Launching fragment com.samsung.android.settings.deviceinfo.SoftwareInfoSettings
Unfortunately, starting the SubSettings from uid except launcher's uid isn't possible, because it is not an exported activity to be visible from the outside:
AndroidManifest.xml:
<activity android:name=".SubSettings"
android:parentActivityName="Settings"
android:theme="#style/Theme.SubSettings"/>
If you try to run:
Intent intent = new Intent();
intent.setClassName(
"com.android.settings",
"com.android.settings.SubSettings"
);
intent.putExtra(
":settings:show_fragment",
"com.samsung.android.settings.deviceinfo.SoftwareInfoSettings"
);
startActivity(intent);
will see this error log:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.aminography.settingsapp, PID: 14566
java.lang.SecurityException: Permission Denial:
starting Intent { flg=0x10000000 cmp=com.android.settings/.SubSettings (has extras) }
from ProcessRecord{fac1d09 14566:com.aminography.settingsapp/u0a104} (pid=14566, uid=10104)
not exported from uid 1000
Unfortunately, since isValidFragment() and SettingsGateway#ENTRY_FRAGMENTS are parts of the platform, not your application's runtime, it's impossible to change them even with reflection.
As the android documentation says, you should use
Intent i = new Intent(Settings.ACTION_DEVICE_INFO_SETTINGS);
startActivity(i);
https://developer.android.com/reference/android/provider/Settings#ACTION_DEVICE_INFO_SETTINGS
If it doesn't work for SAMSUNG devices I'm afraid it will probably not be possible. At least with no official answer. Many times vendors modify stock Android so for SAMSUNG devices settings app is different than stock AOSP, and the official method will not work.

Android java trouble with start new activity

In MainActivity starts another activity via startActivityForResult
Intent intent = new Intent(MainActivity.this, AutorisationForm.class);
intent.putExtra("req", 1);
startActivityForResult(intent, 0);
Method onCreate executes successful, activity displays on the screen and then app crash. Eclipse returns "source not found" error.
How I can solve this problem?
P.S. All activities declared in manifest.
P.P.S All worked successfully before I add a lot of logic in MainActivity. This code and second class hasn't changed.
Problem solved. Mistake was in onDestroy() method in MainActivity. In this method app try to save unloaded file. Theme closed.
Undo all changes before adding "a lot of logic in MainActivity."
Add changes one at a time and test in between until you discover problem.
Problem discovered!

DisplayMessageActivity cannot be resolved to a type-Building first android App

Hi I have just began working on the first android application on developer.android.com.
well to start with I got to learn many error origins and their solutions from S.O. , but i have been trying to figure out this statement
"DisplayMessageActivity cannot be resolved to a type" while we have to set an Intent for button onclick function. It shows this error in the line where the code line is:
Intent intent = new Intent (this, DisplayMessageActivity.class);
here is the java file:
MainActivity.java
}
/**called when the user clicks the send button*/
public void sendMessage(View view) {
Intent intent = new Intent (this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById (R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity (intent);
}
}
I tried a lot find which class do I need to import now, and searched but no avail.
may be I am a beginner is what I miss here.
Well, I think it's too late to answer and probably you figured out already.
However, just in case I'd like to put some more explanation.
Probably their "Starting Another Activity" section of Buidling Your First App" was revised after you posted your question, but I found that the user-defined "DisplayMessageActivity" is defined several lines below where you were guided to write code to create Intent and thus refer to DisplayMessageActivity.
At "Create Second Activity" section, the DisplayMessageActivity is created.
Well, Google's pedagogy style is not good, and I found out that their framework design ( and thus naming ) is not good and doesn't reveal what they are.
But.. if you choose Android platform to develop for, what can you do other than endure that. Good luck with that.
In that tutorial of developing first app, they create DisplayMessageActivity.java later part of tutorial. Please read the complete tutorial. The documentation have been corrected to indicate the same when using IDEs. You can visit here :
Note: The reference to DisplayMessageActivity will raise an error if you’re using an IDE such as Eclipse because the class doesn’t exist yet. Ignore the error for now; you’ll create the class soon.
http://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent
DisplayMessageActivity is not a class predifined by android packages, so you should create it as ordinary java class and call from yours, here, MainActivity. Sure it doesn't require to be named as in the tutorial
You can create the display message activity class yourself by adding this code anywhere
public class DisplayMessageActivity {
}

Puzzling java.lang.NullPointerException in Android App

I'm currently working on an Android App and, almost every time I use it, I get a an error. Most of the time it doesn't crash the app, but it is still frustrating that I'm seeing this error. I thought I did a check for it, but I could be wrong. Thanks for the help! The relevant code is below. (It's relevant because the line outside the if statement is throwing the NullPointerException.)
Activity activity;
if(activity == null)
{
activity = new Activity();
}
Intent intent = new Intent(activity, Service.class);
You don't usually instantiate the Activity class in this manner. Please see the documentation on the Android Activity class here:
http://developer.android.com/reference/android/app/Activity.html
You should post some more of the surrounding code, but your problem is that creating new Intent requires a valid Context. Usually you create an Intent within an Activity (or Service or BroadcastReceiver) class so you can just do something like:
Intent intent = new Intent(this, Service.class);
Occasionally you'll create it somewhere else and pass it that valid Context, but you should pretty much never create an Activity by calling the constructor directly. There's a lot of other initialization necessary to make it useful.
As postet previously there is more to initiate for an activity than calling the constructor. Probably you get a null pointer exception deep within the Intent Constructer where it is trying to get some of the Activityinformation usually provided.
If you really want to create a Service, heres a link for starting a Service, but you should really read the whole article and probably some more of the activity lifecycle ressources.
http://developer.android.com/guide/topics/fundamentals/services.html#StartingAService

How do I use Startactivity and leave the calling Cocos2d Layer active (Android/Java)?

I'm trying to port my iPhone Cocos2d game to Android using the Cosos2d for Android framework (Java).
I'm running into a problem when I start PreferenceActivity from my main activity. This is my setup:
(1) HelloWordActivity.java:
Inits the Cocos2d environment and starts GameLayer.scene
(2) Gamelayer.scene:
Presents the user with the game but also has a button to show the settingspane which gets called from the Gamelayer class like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(helloworldactivity, ShowSettingsActivity.class.getName());
helloworldactivity.startActivity(intent);
This shows the Preference Activity just fine, but when closing the preferences and returning to my game it appears it did not persist (it get's reloaded/restarted and does not retain it's state).
How can I present the user with a standard Android preferences screen without destroying the current game session in Cocos2d ?
from the names of your classes , I'm assuming you used Dan's tutorial as a starter
make sure you remove dan's onStop() override method , that gets called when you are loading the preferences and it calls
CCDirector.sharedDirector().end();
removing this should do it for you
I did notice that you gave up and switched to andEngine , that's a nice engine too but I thought I'd answer this one anyway
You need to get the main activity to start new activity, I hope this code will help you
Activity context = CCDirector.sharedDirector().getActivity();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(helloworldactivity, ShowSettingsActivity.class.getName());
context.startActivity(intent);

Categories

Resources