Using "UNINSTALL_SHORTCUT" Intent in Android - java

I am trying to use the following intent action, but i get an
ActivityNotFoundException in the first case (startActivity) and nothing in the second case (sendBroadcast).
"com.android.launcher.action.UNINSTALL_SHORTCUT"
I try to use it with the following code:
Intent i = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
startActivity(i);
Also with the following:
Intent i = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(i);

First you need to have the proper permissions:
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
Then you need to do something like this:
Intent intent = new Intent(this, your main activitiy);
intent.setAction(Intent.ACTION_MAIN);
Intent removeIntent = new Intent();
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
removeIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
removeIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(removeIntent);

Related

How to move from one activity to another i.e Java Class to Kotlin Class in an Android Java Project?

I am using the following ways but in all the cases in the first class which is Java Class, there is an error during run time. Here LoginActivity is the Java class and VerifyMobile Activity is the Kotlin class.
Intent intent = new Intent(this, VerifyMobile.class);(Run Time Error)
Intent intent = new Intent(LoginActivity.this, VerifyMobile.class);(Run Time Error)
Intent intent = new Intent(this, VerifyMobile::class.java); (Compiler Error)
Intent intent = new Intent(LoginActivity.this, VerifyMobile::class.java); (Compiler Error)
Looking for the solution.
Make the changes in the first class which is Java class
Make the changes in the second class which is Kotlin Class.
In 3 and 4, it looks like Kotlin and Java syntax is being mixed together.
To create an intent in a Java file (.java), do this:
Intent intent = new Intent(context, VerifyMobile.class);
To create an intent in a Kotlin file (.kt), do this:
val intent = Intent(context, VerifyMobile::class.java)
It doesn't matter what language the Activity being navigated to is written in. What matters is the language of the file the code is being written in.
To init another activity you should create your intent with the current activity to the next one like this:
Java:
Intent intent = new Intent(this, AnotherActivity.class);
startActivity(intent);
Kotlin:
val intent = Intent(this, AnotherActivity::class.java)
startActivity(intent)
or:
Java
startActivity(new Intent(this, AnotherActivity.class));
Kotlin:
startActivity(Intent(this, AnotherActivity::class.java))

Android Studio: How to send, and recieve a Class in diferent Activities

Im making a quiz game with questions on diferent topics
For Example i have activities for these topics: Flags, Capitals, Population, Economy, Continent, etc.
And i have one single ResultActivity to obtain the Score of the quiz.
The ResultActivity has a PLAY AGAIN button.
On the FlagsActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "FlagsActivity");
startActivity(intent);
On the CapitalActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "CapitalActivity");
startActivity(intent);
etc.....
On the ResultActivity i have this code:
activity = getIntent().getStringExtra("NAME_ACTIVITY");
public void playAgain(View view){
if(activity.equals("FlagsActivity")){
Intent intent = new Intent(getApplicationContext(), FlagsActivity.class);
startActivity(intent);
}
if(activity.equals("CapitalActivity")){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
if(activity.equals("PopulationActivity")){
Intent intent = new Intent(getApplicationContext(), PopulationActivity.class);
startActivity(intent);
}
if(activity.equals("EconomyActivity")){
Intent intent = new Intent(getApplicationContext(), EconomyActivity.class);
startActivity(intent);
}
if(activity.equals("ContinentActivity")){
Intent intent = new Intent(getApplicationContext(), ContinentActivity.class);
startActivity(intent);
}
}
Basically im sending an Intent with a String containing the name of the activity, then on the Result Activity evaluating with "if" the String = That activity name, start the activity.
What i want to do is someting like this:
On the Flags Activity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(FlagsActivity.class);
startActivity(intent);
On the CapitalActivity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(CapitalActivity.class);
startActivity(intent);
On the Result Activity:
activity = getIntent();
public void playAgain(View view){
Intent intent = new Intent(getApplicationContext(), activity);
startActivity(intent);
}
So that i can create as many quiz activities without having to create an "if" statement on the ResultActivity for it to work.
You can pass the class name as a string and use reflection to look up a class object for that type. Something like this:
Class classToLoad = Class.forName(getIntent().getStringExtra("NAME_ACTIVITY"));
Intent intent = new Intent(getApplicationContext(), classToLoad);
startActivity(intent);
I would pass the fully qualified class name to avoid errors.
You could pass the String of activity name to the Result activity, and get its corresponding class name using reflection:
String strActivity = "com.package.FlagsActivity";
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("activity_name", strActivity );
startActivity(intent);
String activityName = getIntent().getStringExtra("activity_name");
Class<?> myClass = Class.forName(activityName );
Intent myIntent = new Intent(getApplicationContext(), myClass);
Something like this.
In the Results Activity
public static void toActivity(Context context, final Class ActivityToOpen){
//whatever awesome code you would like to perform
Intent intent = new Intent(context, ActivityToOpen);
startActivity(intent);
}
In the originating Activity (Flags Activity for example)
ResultsActivity.toActivity(FlagsActivity.this, FlagActivity.class);
Check for typos... I kinda winged this :)
You should be able to call from any originating Activity without if statement.
When you receive the data
String activity;
Bundle bundle = getIntent().getExtras();
if (bundle != null){
activity = bundle.getString("NAME_ACTIVITY");
}

Store intent for one activity but start a different activity

I am new to android and I have a problem. I want to store intent for an activity but start a different activity. I have a backgroundworker activity that has to store intent for postAnnotationActivity, but has to start InstructionsActivity. If I use intentID.getStringExtra in PostAnnotationActivity then the output = null. Can somebody help?
Part of code from Backgroundworker:
Intent intent = new Intent(context, InstructionsActivity.class);
Intent intentID = new Intent(context, PostAnnotationsActivity.class);
intentID.putExtra(ID, id);
context.startActivity(intent);
part of PostAnnotationActivity:
Intent intentID = getIntent();
String getID = intentID.getStringExtra(BackgroundWorker.ID);
From InstructionsActivity I'll go to StartActivty using startActivity(new Intent(this, ShowPaintingActivity.class)and then to PostAnnotationActivity using startActivity(new Intent(this, PostAnnotationsActivity.class));
Try storing your Intent instead as a string and use shared preferences to store/retrieve your Intent. Like so.
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("StoredIntent", "String you want to store here");
editor.commit();
Modify your code as following:
In Backgroundworker:
Intent intentID = new Intent(context, InstructionsActivity.class);
intentID.putExtra("ID", id);
context.startActivity(intentID);
In InstructionsActivity:
Intent intentID = getIntent();
String getID = intentID.getStringExtra("ID");
Intent intent = new Intent(context, ShowPaintingActivity.class);
intent.putExtra("ID", getID);
context.startActivity(intent);
In ShowPaintingActivity:
Intent intentID = getIntent();
String getID = intentID.getStringExtra("ID");
Intent intent = new Intent(context, PostAnnotationActivity.class);
intent.putExtra("ID", getID);
context.startActivity(intent);
In PostAnnotationActivity:
Intent intentID = getIntent();
String getID = intentID.getStringExtra("ID");
And another better solution is, as user1375469 suggested, you can save value to preferences in Backgroundworker and then retrieve it from PostAnnotation Activity.
Your are getting the output null because you are not starting the Activity i.e.
context.startActivity(intentID);
while you are passing the data into it..
and you want to go to my InstuctionsActivity but already store some data from BackgroundWorker for PostAnnotationActivity....
So, you have to use SharedPreference for this purpose....

Copy intent / change source activity and destination class

I want to change to a new Activity over an Intent, but in my current Intent there are all my ExtraStrings as parameters. So decided to copy it with
Intent next = getIntent();
but how to change the source activity and the destiantion class now ?
Probably the better solution would be create your own Intent with your destination class and use Intent.putExtras(Intent src) to put extra data of original intent to new one.
And of course you can use Intent.setClass or setClassName() just to replace destination class.
You can also pass your data in bundle..
FirstActivity:
Intent mIntent = new Intent(this, activity1.class);
Bundle mBundle = new Bundle();
mBundle.putString("key", "value");
mIntent.putExtra("keyBundle", mBundle);
SecondActivity:
Intent mIntent = new Intent(this, activity2.class);
mIntent.putExtras(getIntent().getExtras().getBundle("keyBundle"));

How do i launch the music player

How am i suppose to use this
android.intent.category.APP_MUSIC
to launch the music player?
it doesn't work if i call makeMainSelectorActivity
Intent intent = new Intent();
intent.makeMainSelectorActivity(intent.ACTION_MAIN,
"android.intent.category.APP_MUSIC");
startActivity(intent);
This solution might help someone looking to launch default Music Player:
if(android.os.Build.VERSION.SDK_INT>=15){
Intent intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
Intent.CATEGORY_APP_MUSIC);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Min SDK 15
startActivity(intent);
}else{
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");//Min SDK 8
startActivity(intent);
}
Used below code :
Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
or
Intent intent = new Intent("android.intent.action.MUSIC_PLAYER");
startActivity(intent);
Ans also prefer url :
Launching the default music player

Categories

Resources