http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Page says that, I have to use Intent
I can't use Intent because my code have only one Activity. I want to use notification to get back to app e.g. from DEVICE'S HOME SCREEN.
I have to use Intents when I want to execute something by clicking the notification. Is there a way to use setContentView after clicking the notification?
Without seeing what you have attempted so far, I am going to take a stab at this one.
I don't quite see why regardless of how many Activities you have in your app, you cannot declare / use an Intent. If you need to trigger your only Activity again, after a Notification has been clicked, and need to call the setContentView(R.layout.some_layout_xml); in the Activities onCreate() again, why not declare an Intent for your Notification like this:
Intent intent = new Intent(getApplicationContext(), YOUR_ACTIVITY.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(YOUR_ACTIVITY.this, 0, intent, 0);
This way, clicking on your Notification should do what you want it to do.
Again, this may or may not work for you considering that you haven't posted any code at all.
EDIT: Based on the comments by the OP, what I think the solution maybe:
Refer to this link on how to send data with an Intent for a Notification: How to send parameters from a notification-click to an activity?
Basically, since a Notification has been triggered, I am assuming that the user has already logged in. Now to skip the login part when they come back to the app after clicking a Notification, send some extras along with the Intent. Then, when the Activity starts, check for the value and using an if...else statement, decide which layout should be shown.
Again, and I cannot stress this enough, you should always show what you have done so far. That really helps finding a solution. Your actual requirement has no bearing on what you need done at all.
You can use an Intent to get back to the app, e.g. like this:
final Intent intent = new Intent(context, YourActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, intent, 0);
final Notification notification = new Notification(
R.drawable.logo, tickerText, System.currentTimeMillis());
notification.setLatestEventInfo(context, title, text, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
Related
For example, first activity of app is MainActiviy which is basically 2 text fields (login and password) with a button. Pressing a button tells the database these text fields' data so it can tell the program if the user is in database and password is correct.
What I'm used to is to create a bool flag field saved in SharedPreference so the program knows i already logged in my app recently so it can open my second activity, but it seems like wrong and weird solution.
So, how do I properly login into my app in theory?
Mikkel,
Your approach with storing data using shared preferences is perfect but I have some suggestions which can make this solution better and robust.
First of all, you should check the sharePreference value inside a splash screen. Which checks for the login flag and launch the LoginActivity if a user is not logged in and launch the HomeActivity if a user is logged in.
Second, I would suggest using Androidx DataStore, which is a better version of shared preference and it can be asynchronous API so you can use it safely on the UI thread.
Don't forget to close your main activity after user login using the below code.
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If you need an example to handle this stuff better here is the github-sample which can be very helpful.
This is wrong:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
You should use "startActivity" before "finish()"
Correctly:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
My app is tabbed, when I start a new activity over the top of the tabs, and press the back button on the phone to return, it returns to the tab I was previously on. When I go back with this intent:
Intent intent = new Intent(EditViewerActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
It doesn't return to the tab I was on before. Is there anyway to get the same behaviour as the back button on the phone in an intent?
Solution was to use finish();.
finish(); will end the current activity, the proper way would be to use NavUtils.navigateUpTo(this, new Intent(this, MyActivity.class)); which can be used after importing import android.support.v4.app.NavUtils;
They will both basically do the same thing in the end.
I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
I know there have been a lot of posts related to this:
How to know which application the user chose when using an intent chooser?
https://stackoverflow.com/questions/6137592/how-to-know-the-action-choosed-in-a-intent-createchooser?rq=1
How to get the user selection from startActivityForResult(Intent.createChooser(fileIntent, "Open file using..."), APP_PICKED);?
Capturing and intercepting ACTION_SEND intents on Android
but these posts are somewhat old, and I am hoping that there might be some new developments.
I am trying to implement a share action without having it be present in the menu. The closest solution to what I want is provided by ClickClickClack who suggest implementing a custom app chooser, but that seems heavy handed. Plus, it seems like there might be some Android hooks to get the chosen app, like the ActivityChooserModel.OnChooseActivityListener.
I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, shareMessage());
sendIntent.setType("text/plain");
Intent intent = Intent.createChooser(sendIntent, getResources().getText(R.string.share_prompt));
ShareActionProvider sap = new ShareActionProvider(this);
sap.setShareIntent(sendIntent);
sap.setOnShareTargetSelectedListener(new ShareActionProvider.OnShareTargetSelectedListener() {
#Override
public boolean onShareTargetSelected(ShareActionProvider source, Intent intent) {
System.out.println("Success!!");
return false;
}
});
startActivityForResult(intent, 1);
As of API level 22 it is now actually possible. In Android 5.1 a method (createChooser (Intent target, CharSequence title, IntentSender sender)) was added that allows for receiving the results of the user's choice. When you provide an IntentSender to createChooser, the sender will be notified by the chooser dialog with the ComponentName chosen by the user. It will be supplied in the extra named EXTRA_CHOSEN_COMPONENT int the IntentSender that is notified.
I am trying to capture the result of Intent.createChooser to know which app a user selected for sharing.
That is not possible.
Other "choosing" solutions, like ShareActionProvider, may offer more. I have not examined the Intent handed to onShareTargetSelected() to see if it contains the ComponentName of the chosen target, though the docs suggest that it should.
And, if for some reason it does not, you are welcome to try to fork ShareActionProvider to add the hooks you want.
The reason why createChooser() cannot be handled this way is simply because the "choosing" is being done by a separate process from yours.
I have the following code in my MainActivity, but the onShareTargetSelected method is never getting called.
ShareActionProvider goes in the action bar. You cannot just create an instance, call a couple of setters, and expect something to happen.
I'm experiencing the problem described in this Android issue:
http://code.google.com/p/android/issues/detail?id=4536
Simply put, after pressing the HOME button, android prevents services and broadcast-receivers from calling startActivity for 5 seconds.
I've also noticed that (well, theoretically), having the following permission :
"android.permission.STOP_APP_SWITCHES"
allows you to call resumeAppSwitches (which is defined in ActivityManagerService).
Looking at the latest version of ActivityManagerService, this code is removed.
The question: How to launch an activity using startActivity without this 5 second delay?
Here is a solution I found.
Put your intent that you want to start immediately in a PendingIntent, then call the send() Method on it.
So instead of this
Intent intent = new Intent(context, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
just do this
Intent intent = new Intent(context, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent =
PendingIntent.getActivity(context, 0, intent, 0);
try {
pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
e.printStackTrace();
}
I don't think there is a way to do it with the current APIs. I think that is how they intended it to work so that an app cannot force itself back open when the user exits with a home key press. You could add the home/ launcher intent to the filter for whatever activity it is you are trying to start. Then the user would have the choice to basically treat that app as though it is a homescreen. Then it would get launched with no delay at all whenever the user presses the home button(They'd have to select it from the list that will pop up asking which app they want to use to complete this action, but they could check always use this app to take this step away in the future.)
I am using AlarmManager to start Activity immediatly from Service.
Activity starts without delay, even if you have pressed home button before.
Tested on Android 5.0.1 (Galaxy Alpha).
Don't work at 6.0.1 (Nexus 7 2013) :-(
Don't work at 4.1.2 (Galaxy S II) :-(
Don't work at 4.3 (ASUS MeMO Pad FHD 10 ME302C) :-(
#TargetApi(Build.VERSION_CODES.KITKAT)
private void startActivity() {
Intent intent = new Intent(this, Main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
long now = Calendar.getInstance().getTimeInMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, now, pendingIntent);
else
alarmManager.set(AlarmManager.RTC_WAKEUP, now, pendingIntent);
}
I am intrigued by this "feature" also and how to avoid it. Reading the post: http://code.google.com/p/android/issues/detail?id=4536 (read the comment #10).
I quote the relevant part below:
Workarounds are:
1) Don't use an
activity, do everything from a
service.
2) Have some kind of
intermediate Home (WidgetLocker
HomeHelper, QuickDesk, PowerStrip,
etc). These do a startActivity
immediate to start the "real" Home and
this bypasses the 5 second rule. This
is a bad idea as Android prioritizes
keeping the system Home in memory but
not whatever secondary Home the
intermediate set. So it can lead to
Launcher reloads which is no fun. Plus
it's very confusing to users.
3) Root
can start activities during this
period.
Among those, I believe the best way to do it is to create a "Home Helper"-like activity. So, instead of starting a new activity, you would call this one instead. This is specially true, since you are creating a launcher app.
As I said in my previous comment to the question, I would contact the WidgetLocker developer about it. Alternatively, you can use APK Manager to see how he implemented it (he even encouraged the use of the APK Manager to create different mods to his app, the link to the xda-developers thread is in the comment above)
I am new to Android so apologies if I am asking something silly. I am trying to develop an alarm clock application - basically, it's my final project and I am trying to develop an alarm like there is in API level 2.3.3.
I have designed the list view that takes input through a dialog box like time. I have also coded it to set an alarm.
Now I want to save that alarm as an intent in the other class, and I don't have any idea how to save different alarms in the other activity. I have also checked for the desk-clock alarm code but I didn't get that too.
Please help me someone, I am stuck here for the code for more than a week. Please someone help me, I shall be thankful to you.
If you want to send an Intent from one Activity to another, and then retrieve information from inside the Intent, the best way is use the Bundle object inside the intent:
Let's supose you send the intent from Activity1 to Activity2...
In Activity 1:
Intent intent = new Intent(Activity1.class,Activity2.class);
//I use the String class name as a key value, but you can use whatever key
intent.putExtra(String.class.getCanonicalName(), myString);
startActivity(intent);
//Or this other method if you want to retrieve a result from Activity2
//startActivityForResult(intent,Activity2);
In Activity 2:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String myString = bundle.getString(String.class.getCanonicalName());