I'm building an Android application that needs to have customer specific code. Customer specific code needs to be separated from the actual Android product our company supplies.
To do this I tried to create 2 packages:
com.company.product.activity
com.company.product.customcode.activity
Both packages contain ExampleActivity.
I have written a factory that uses reflection to determine of a custom component exists on top of the product class. This works fine.
Using the following code to start the ExampleActivity of product works:
Intent intent = new Intent(this, com.company.product.activity.ExampleActivity.class);
startActivity(intent);
Using the following code to start the ExampleActivity of customcode fails:
Intent intent = new Intent(this, com.company.product.customcode.activity.ExampleActivity.class);
startActivity(intent);
Error:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.product.customername/com.company.product.customcode.activity.ExampleActivity}: java.lang.IllegalArgumentException: AppIndex: The URI host must match the package name and follow the format (android-app://<package_name>/<scheme>/[host_path]). Provided URI: android-app://com.company.product.customcode.activity/http/host/path
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
I also tried this code, but then Android gives a Toast that it cannot find the Activity:
Intent intent = new Intent();
intent.setClassName("com.company.product.customcode.activity", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
Manifest:
<activity android:name="com.company.product.activity.ExampleActivity"
android:label="#string/app_name"
android:noHistory="false"
/>
//Custom implementation of the ExampleActivity
<activity android:name="com.company.product.customcode.activity.ExampleActivity"
android:label="#string/app_name"
android:noHistory="false"
/>
Does anybody have any ideas or tips how to achieve the maingoal: To split custom code from the productcode where activity names might be equal.
I am not sure what you missed here. I have tried this and it worked.
I have created a class MainActivity under package com.sample.so_sample.activities
and another MainActivity under package com.sample.so_sample1.test.activities
My manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.so_sample">
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.sample.so_sample1.test.activities.MainActivity">
</activity>
</application>
</manifest>
and my call to navigate in com.sample.so_sample.activities.MainActivity is
Intent i = new Intent(this, com.sample.so_sample1.test.activities.MainActivity.class);
startActivity(i);
There is nothing wrong in the code.I had tried the same,it seems no issue.
Differen package names is the right way, it has to work fine, seem like problem is out of this code.
Your code can be in any java packages, you need just specify fully qulified name of activity in manifest. Android package name it's just a unique app id string.
instead of:
Intent intent = new Intent();
intent.setClassName("com.company.product.customcode.activity", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
try:
Intent intent = new Intent();
intent.setClassName("YOUR PACKAGE NAME IN MANIFEST", "com.company.product.customcode.activity.ExampleActivity");
startActivity(intent);
I've solved this question.
It seemed by further investigation that the Activity crashed because of some auto-generated code that was added while copy-pasting....
It seemed like a crash before the Activity was loaded, but this apparently was not the case.
Thank you all for you're answers, they were very useful!
Related
Good morning,
Quick coding inquiry I'd like to put forth.
I've gotten an assignment to produce an application on Android Studio that has two buttons that should each load new pages when clicked.
The chapter from our books explains how to do this with one button, but I assume there might be a different set up with two buttons?
Anyway, this is only the second assignment in our class so I'm not very far in understanding how all of this works; that and the class is an Independent Study course so I'm teaching myself. Not always the easiest thing.
Anyway, here's what I do have:
MainActivity.java
package com.example.thelatestmusicscene;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.buttonOne);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MusicNewsOne.class));
}
});
Button button2 = (Button) findViewById(R.id.buttonTwo);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MusicNewsTwo.class));
}
});
}
}
I believe this is the only file that's relevant to my problem but I can post the others if you need to see them too.
The first button functions perfectly but when I try to use the second button the app stops working. I've seen a few explanations online, but the few I've found about loading other pages gives me a bunch of errors in my code.
Note: This is how we were shown to do it from the book, but I'm all for alternative methods.
Everything else for this application is done, it's just this linking to the second java page I'm faltering on. If you know a better way to lay out the code I'm all ears or if you could link me to a resource that could teach me, I'd be grateful.
OK, this is going to be the confusing part. When I downloaded Android Studio for the class I could never get any of the emulators to run. This was a problem I also posted, which as of today, has still not received an answer. If you have a solution to that or want to see those details of what I’ve tried, go here:
https://superuser.com/questions/1394568/android-studio-and-haxm-installation
Ultimately, I could write code, but I couldn’t test it. However, I found a way to run the apps on my own smartphone to test if they’re functioning. Letting Android Studio build the APK file and then letting my phone run the app.
Now…as for errors. There are none. Android Studio isn’t coming back with any. As far as it's concerned, everything is fine. When I load the application on my phone and I click the second button, the application closes, and I get “Unfortunately, nameOfApp has stopped.”
And that’s where I find myself.
Here’s the XML (I assume you mean the AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thelatestmusicscene">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MusicNewsTwo">
</activity>
<activity android:name=".MusicNewsOne" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I've got to get some sleep now but I'll try retrieving a crash report from my phone today if I can.
Brother your MainActivity.java file have correct code.
Kindly check button name in xml and java file, and check your manifest file either MusicNewsTwo added as activity.
And be sure you have both java and xml file in correct place.
Few notes
You may need to provide the crash's log/stacktrace/message (for us to know what's the solution for it)
You may also need to provide us the layout or the .xml file
What could possibly went wrong
One of the common problems encountered:
Assuming you have only copied it from your class, you might have encountered the error saying that your MusicNewsTwo.class is not declared in the AndroidManifest.xml, do this:
What could be the possible solution
<application
android:label="Example APp"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="package.path.to.MusicNewsOne">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Add this activity if missing -->
<activity
android:name="package.path.to.MusicNewsTwo"/>
</application>
I will update my answer so as long as you take into consideration the Few notes section of my answer, otherwise if I got your answer write already with this one, comment out.
Found the problem within my phone itself. Turns out it was a permission error for downloading 3rd party apps; my code was actually perfect. Thanks for all the suggestions though.
I think the IDs of the views in the layout : "activity_main" are different than the ones you are using in the MainActivity.java file
Bro there are two ways to do this, start a new Activity like you are doing, and other is By Using one activity you can post the fragments on the same activity as much as you want i.e 1 ,2 3 and so on.here is the link for fragments, but you are very much biggner ill suggest you to start with the actitvity and learn about the Activity life cycle. you can find here . moreover tell me what the error you are getting, To find the error see a the bottom of android studio the option (Logcat) click on it . and see the error and past it in the comment.
I am trying to add a short on home screen programmatically but it wont work.
The code works for Oreo and above but not below that.
public void addShortcutToHomeScreen() {
if (ShortcutManagerCompat.isRequestPinShortcutSupported(this)) {
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(this, "#1")
.setIntent(new Intent(this, ActivityMemes.class).setAction(Intent.ACTION_MAIN)) //!!! intent's action must be set on oreo
.setShortLabel("Memeify")
.setIcon(IconCompat.createWithResource(this, R.mipmap.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(this, shortcutInfo, null);
} else {
Intent shortcutIntent = new Intent(getApplicationContext(),
Activity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.mipmap.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
getApplicationContext().sendBroadcast(addIntent);
}
}
Here is my Manifest file.
<activity
android:name="com.my.package.Activities.Activity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Please, Tell me what am I doing wrong cause I have tried to fixed it and have failed. And is there any better way than this? Any help would be appreciated thanks.
EDIT :
Found this in the Logcat.
2019-01-16 13:27:46.773 735-13377/? W/BroadcastQueue: Permission Denial: broadcasting Intent { act=com.android.launcher.action.INSTALL_SHORTCUT flg=0x10 launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } (has extras) } from com.chameleon.memeify (pid=14824, uid=10300) requires com.android.launcher.permission.INSTALL_SHORTCUT due to receiver com.sec.android.app.launcher/com.android.launcher3.home.InstallShortcutReceiver
you can use this method in your application class
public void createShortCut() {
Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
shortcutintent.putExtra("duplicate", false);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(),
R.drawable.logo);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext(), SplashActivity.class));
sendBroadcast(shortcutintent);
}
}
and apply this permission
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
If your are on Android 8+ you can't use the broadcast anymore and you have to use the ShortcutManager :
https://developer.android.com/about/versions/oreo/android-8.0-changes#as
Android 8.0 (API level 26) includes the following changes to app shortcuts:
The com.android.launcher.action.INSTALL_SHORTCUT broadcast no longer has any effect on your app, because it is now a private, implicit broadcast. Instead, you should create an app shortcut by using the requestPinShortcut() method from the ShortcutManager class.
The ACTION_CREATE_SHORTCUT intent can now create app shortcuts that you manage using the ShortcutManager class. This intent can also create legacy launcher shortcuts that don't interact with ShortcutManager. Previously, this intent could create only legacy launcher shortcuts.
Shortcuts created using requestPinShortcut() and shortcuts created in an activity that handles the ACTION_CREATE_SHORTCUT intent are now fully-fledged app shortcuts. As a result, apps can now update them using the methods in ShortcutManager.
Legacy shortcuts retain their functionality from previous versions of Android, but you must convert them to app shortcuts manually in your app.
To learn more about changes to app shortcuts, see the Pinning Shortcuts and Widgets feature guide.
I am attempting to enable Bluetooth on an Android device. I have read to Android documentation and have a pretty good idea of what the process is. I am, however, rather stuck at actually firing off an Activity using the manifest file. This is what I've done so far...
I've developed an Android Module with a couple of classes:
BluetoothModule // extends KrollModule
BluetoothSetup // extends Activity
In BluetoothSetup, the onCreate method looks like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
connectBluetooth();
}
Also in BluetoothSetup, the connectBluetooth() method looks like:
protected void connectBluetooth(){
// if statements to check if bluetooth is enabled/avail removed
Intent intentBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intentBluetooth, 0);
}
}
Finally, in the module's timodule.xml I've added:
<activity android:label="#string/app_name" android:name="com.eyesore.bluetooth2.BluetoothSetup">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
The module compiles just fine, but does not actually do anything. I fear that I've missed a fundamental step here, but I'm just not sure what it is. Any advice would be greatly appreciated!
Figured this out. Wound up creating a custom per instructions here:
https://wiki.appcelerator.org/display/guides/Maintaining+a+Custom+AndroidManifest.xml
I removed the extra code Titanium drops in the manifest file and it seems to be working.
I have two sperate applications and I want to call start an activity from the second application in the first, here is my code to do so :
Intent intent1 = new Intent(Intent.ACTION_MAIN);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setComponent(new ComponentName("org.two.three.application","org.two.three.application.one));
Context H= context;
H.startActivity(intent1);
And in the android manifest of the project I have this code, I have the line :
<activity android:name=".one">
</activity>
But I keep getting a runtime error, logcat says :
"Unable to find explicit activity class
{org.two.three.application/org.two.three.application.one}; have you
declared this activity in your AndroidManifest.xml?"
Can anyone see my error? The only thing I can think of is my package of the first activity is org.two.three.Class while the second is org.two.three.application.SecondClass. Does this matter?
Thanks in advance
At first try removing those code that you are adding
**
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setComponent(new ComponentName("org.two.three.application","org.two.three.application.one));
Context H= context;
**
Then add following code into an action method like onClick
Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);
Add your Android Manifest configuration file
<activity android:name="NewActivity"></activity>
You just need to make your activity publicly available. To do that just add
android:exported="true"
to the <activity> tag in your manifest.
Normally, activities are not available to other components that are outside of the package. This is the standard default behaviour. But, of course, you can make them available if you want to.
It it possible to start an adobe air apk from my current android app?
I tried this code but I got ActivityNotFoundException:
Intent intent = new Intent();
intent.setAction("air.caarsvcmobile.debug");
startActivity(intent);
Manifest:
<activity
android:name="caarsvcmobile"
android:label="CAARS Video Chat" >
<intent-filter>
<action android:name="air.caarsvcmobile.debug" />
</intent-filter>
</activity>
Is it possible to check package name and class name of an Air app?
After some Googling I found the solution of my problem:
Since I only knew the package name of the apk file I could use this code to launch the default launcher activity.
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("air.caarsvcmobile.debug");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
Thats all. I works fine.