Launch preferred application upon long clicking a button - java

So I have a button, when its pressed once it opens the calendar application, however some of my other other devices don't have the calendar I have described in the method so I have created an onLongCLick method, so when a user long clicks the button, they get to choose which app they want to launch. Once they choose the app, its set. So when they click button normally it launches the app they chose. How can I go about achieving this ??
Thanks for your time.
My longclick methodC
// Hold (onlongclick) to initiate choose app to launch
date_launch.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View arg0) {
// choose app to launch and save it
return true;
}
});

Find the available activities via PackageManager.
public abstract List<ResolveInfo> queryIntentActivities (Intent intent, int flags)
Create a dialog for choosing apps.
Once the app is chosen, launch the package in onClick.

Related

Android Application for Kids

I'm Developing Android Application for Parental Controlling, so I want to disable home and back button. Because kids are unpredictable, their can push home button or back button and so on. Than i want to disable sliding notification bar. (Because notification bar can change device configuration and setting)
I'm planning to create Android Application which is similar to the Samsung Kids Mode-Parental Control.
Is possible to create application like Samsung Kids Mode? Can you give some link/article or even example program to me?
I Have tried this :
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
but it not handle home button, the Home button still working.
Thanks in advance to everyone for taking time to read this, hoping a positive solution.
I dont know of anyway to override the home button. Im not sure that it can be done. You can override the back button like so.
#Override
public void onBackPressed() {
//do nothing, important dont call super.onBackPressed
}
Another this you can do is set the app to immersive mode to reduce the chance of accidently exiting the app
https://developer.android.com/training/system-ui/immersive.html
Extended from Activity
#Override
public void onBackPressed() {
}
can be used to override back button. Be sure to remove super.onBackPressed().

Pause Android app-flow

When a return button is pressed in the app I am working on:
public void clickReturn(View view)
{
// ...
// Call an event handler before finishing the activity
_appData.CustomDetail.hookBeforeFinish(this, new ArrayList<CompSubmission>(_submissions));
// Finish this activity
finish();
}
I am not able to modify clickReturn() - the way the application is structured, all I have access to is the hook hookBeforeFinish().
What I want to do is add a dialog to prompt for some input. I can do this in hookBeforeFinish(), but it only appears for a split second. I assume what is happening is I set up the AlertDialog builder, call builder.show() but finish() is being called directly afterwards - so the dialog only appears momentarily.
Is there anything I can put after builder.show() in the hook function such that it won't continue execution to finish()? If I can pause the application flow until someone presses the OK button on the dialog thatwould work better for me.
Thanks
Please comment finish().
Then read and add a dialog as explained here. In your positive button click (e.g. OK button), add finish();
Edit
Since you cannot override clickReturn() or change the class, you can use your hookBeforeFinish() to create a dialog and comment 'clickReturn();' line.
In your dialog, in the listener for the button which expects to finish the activity add clickReturn();.
This way you will pause the activity closing until user decides to do so.

How to open not main activity when restart my application in android

I made an app worked in background using background service when the user clicked specific button (start button) , the user can stop the application task by restart this application and open it again .. then the user can stop the app by clicking on the stop button.
I use this code snippet to let the app close its UI and return to home screen and before that set the start button on disable mode ...
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Unfortunately, when I open my app again I start from the main activity which is not contains the start button and stop button and when I swipe to the second activity that contains the buttons I find the start button enabled i.e. the previous task of the background service is lost ??!!
could any one provide me by the solution.
Every time you open your app the buttons are enabled by default(excepting those which are disabled from the start).
So you need to saved this state to retain it's state.
Here
is how to retain and restore the state.
You have to take a look at Android application lifecycles, when your app goes to the background it will get to the state onPause() and later to onStop(), what you want is saving your application state for later use, take a look at this thread: Saving Activity state in Android I think the top answer is what you want to do.
You have to save the status of your button, and then load the status later.
You could use SharedPreferences to store your wanted activity.
Then check it right after MainActivity start, to decide change to new activity or not
//check when start app
public class MainActivity extends Activity{
public static SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
sharedpreferences = getSharedPreferences("WantedActivity", Context.MODE_PRIVATE);
if (sharedPreferences.contains("SavedActivityName"))
{
if(sharedPreferences.getString("SavedActivityName","").equals( "ActivityName"))
{
Intent intent = new Intent(getApplicationContext(), ActivityName.class);
startActivity(intent);
}
}
}
}
//save wanted activity
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("SavedActivityName", "ActivityName");
editor.commit();

Prevent back button after logout in android

After logout the user is directed to login screen in android. Now, if user clicks on back button of phone it should stay on login screen itself.
How can I make it possible in android?
I have used following code in my application but it will close my application. It should stay on the login screen only
Intent objsignOut = new Intent(getBaseContext(),Hello.class);
objsignOut.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objsignOut);
Please guide me the correct way.
override the onBackPressed in your login activity, to do nothing..
public void onBackPressed() {
//do nothing
}
It seems to me that there are simpler and cleaner solutions than overriding onBackPressed method, as mentioned here and here.
You can provide flags when launching a new activity (on login or logout) to simply clear the "back-stack" rather than override the behavior for the back-button:
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
This is a safer solution that can also be used after you log-in and not just after you log-out.
public void onBackPressed(){
if(appCanClose){
finish();
}
}
These functions can exist in both the system framework (used if not in your code), as well as in your code. If you leave it empty, the app will do nothing when the back button gets pressed.
In this example, when the boolean value appCanClse is true, the back button will quit the app, if false, the back button wil do nothing. I would make sure the user still has someway to quit the app. :p
You can do it by just adding this two line of codes
#Override
public void onBackPressed(){
moveTaskToBack(true);
}
It will prevent going back to previous activity as well as take the app to background when anyone hits back button
The actual solution is
#Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
}
add this code in Login Activity. App closes when back button clicked in login page.

ActionBarSherlock - Share Content icon issue

I am using ActionBarSherlock and have implemented ShareActionProvider.
When a user selects an app to share content with, eg Twitter, the actionbar displays a Twitter icon next to the Share button icon. This stays there forever.
Does anybody know how to disable the application icon from appearing next to the Share button?
Found the answer:
Implement OnShareTargetSelectedListener and set it on the
ShareActionProvider
public void onCreateOptionsMenu(Menu menu){
....
actionProvider.setOnShareTargetSelectedListener(this);
....
#Override
public boolean onShareTargetSelected(ShareActionProvider source,
Intent intent) {
context.startActivity(intent);
// started activity ourself to prevent search history
return true;
}
The top target is featured in the action bar. This is the behavior of the widget as it exists in Android.
If you do not want this behavior copy the sources into your app and modify its behavior to never display the top target icon.

Categories