onBackPress in Service - java

I need to somehow press the back button, but do it programmatically, without pressing the physical button, or play finish(), only in another application.
I found this: Trigger back-button functionality on button click in Android, but this reproduces the click from activity, and I need from service
public class AutoService extends AccessibilityService {
#Override
public void onBackPressed(){
super.onBackPressed();
}
}
Error, writes that there is no such method

Related

How to update variable inside of a service and execute based off of the status

I am learning Java and Android development. Right now I am making an app that once launched will create a service. Then I want the service to do things based on a button that I press within my app.
This is my service class.
public class ServiceClass extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
// Issue is here. I cannot use findViewById
Button start_stop_button = (Button) findViewById(R.id.button2);
start_stop_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
// stop the wifi manager
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
My thought was I could just set a listener so when I press the button the onClick() would execute. But that wouldn't work since the service is not attached to any activity to know what the id "button1" is.
So then I thought about keeping a class variable and just updating it. These seems fairly simple but then I am not sure how I would be able to keep the service checking for the status of the variable to change. I could put it in a for loop to continue checking but I feel like a better way exists.
tldr; I have an app that kicks off a service. I want my app to be able to be closed and the service still run. But I want to have a start/stop button in my app that will trigger the service.
You don't need a button as such to explicitly stop a service because it will be stopped automatically when the job inside onStartCommand is done
If you want manual control for the service inside your Activity where your button actually is then you can use stopService and startService methods
You can find a detailed explanation here
Also Service is meant to be Ui-less, so you can't do something related to the UI in the service, that's not what it is meant for. You can pop a notification and on tap you can start your activity which can give you access to the button if you're wishing to do something like that

How do I set a ClickListener in a Activity to a Button that belongs to a Fragment? (Android studio Java)

i need some help. I'm making an app on android studio and I have a Button initialized in a Fragment. I need to call this button in another Activity and set a click listener on it.
The problem is that when I do this I get the .NullPointerExceptionError and it says that the value of the object is null.
Now i tried to initialize the button in both my fragment and Activity but I keep getting that error and the app crashes.
I need some help to call this button from the Fragment. Maybe my question is similar to some other but it's hours that I'm searching a solution to this problem and I found nothing.
Thanks in advance for all your help
Since the button is part of the fragment, it only fits that any interactions/modifications to the button be defined in the Fragment's scope itself.
There are 2 ways to go about it:
Create the click listener in the Fragment itself and navigate to another activity from there.
Create an interface which your activity implements and the fragment has an instance of that interface. On the button click, call the interface's method. That would give you control in the activity which is hosting the fragment.
MyActivity extends AppCompatActivity implements MyClickListener {
void onMyButtonClick() {
// Navigate to another activity here
}
}
MainFragment extends Fragment {
private MyClickListener listener;
void onAttach(Context context) {
listener = (MyClickListener) context;
// Now call listener.onMyButtonClick() from button's on click listener.
}
}
interface MyClickListener {
void onMyButtonClick();
}

Should DialogFragment be called through AsyncTask class?

I read many guides but I'm still confused.
Somewhere I read that the "activity flow" should not be interrupted by a DialogFragment, so you should call DialogFragment inside a AsyncTask Class inside the Activity Class.
In other guides I saw DialogFragment being called from the Activity Class without using AsyncTask.
So my question is: should DialogFragment be called only through AsyncTask class?
This is the way I did so far; the Activity class code:
public class LunchActivity extends AppCompatActivity {
....
public void callDialog(){
class ShowInfoToUser extends AsyncTask<Bundle, Void, Bundle> {
...
#Override
protected Bundle doInBackground(Bundle... args) {
...
}
#Override
protected void onPostExecute(Bundle resultBundle) {
DialogFragment permissionDialogManager= permissionDialogManager.newInstance(messageBundle);
permissionDialogManager.show(activity.getSupportFragmentManager(), "Permission Dialog");
}
}
}
This is the class that extends DialogFragment:
public class PermissionDialogManager extends DialogFragment {
public static PermissionDialogManager newInstance(Bundle bundle) {
PermissionDialogManager frag = new PermissionDialogManager();
frag.setArguments(bundle);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
}
}
Thank you
The code inside AsyncTask's onPostExecute method execute in the UI Thread. In the example you have provided, there is not difference if you use AsynTask or not, because the code will be executed in the UI Thread.
Maybe in the example you have seen, they process some information in the AsyncTask's doInBackground method (that execute in separate thread) and later in the onPostExecute method they use the previous information to invoke the DialogFragment.
How to know when should you run code in the UI Thread?
Processes and Threads
When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user's perspective, the application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog. The user might then decide to quit your application and uninstall it if they are unhappy.
As the Dialog will interrupt the user, I see no reason to put it in an AsyncTask. The dialog is not supposed to take a huge amount of time to generate itself.

How to detect if whole application is close in android

Some times application be closed by pressing home button and onDestroy() doesn't call. I want to call a method when whole application is closed and I'm not going to call my method in all activities's onDestroy().
implements LifecycleObserver inside appication class an then use as blow:
public class App extends Application implements LifecycleObserver{
#SuppressLint("CheckResult")
#OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onMoveToForeground() {
}
#SuppressLint("CheckResult")
#OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onMoveToBackground() {
}
}
Also can use other events like Lifecycle.Event.ON_DESTROY or ON_CREATE
Application does not close on home button press.But it goes in background.
When you Application goes in background(Your front activity goes in background) it calls onStop() Method(Activity is not visible now).So you should do all stuff here.
There is no such call back the Application class. Which tell you that application is destroyed.
If you want to fire an event when application is fully closed.You should check your application's activity stack.If it does not have any activity than your application is closed. You should check it from a service.

Javafx : Disabling Menu (MenuButton) auto close

I have a MenuButton that contains only CheckMenuItems. My user will usually check several items and if he has to re-open the menu for each one, he will soon throw his mouse through the screen.
I choose to use a menubutton rather than a combobox because it seems that it's not possible to put checkboxes into a combobox (https://community.oracle.com/thread/2598157).
Anyone has an idea ?
Thank you very much,
Léo
Consider in addition to the menu items to provide a toolbar with toggle buttons.
Note: Drombler FX provides an action framework to keep the state and logic between menu items and toolbar buttons in sync. It supports CheckMenuItems and toggle buttons as well.
Disclaimer: I'm the author of Drombler FX.
Getting Started: http://wiki.drombler.org/GettingStarted
Blog: http://puces-blog.blogspot.ch/search/label/Drombler
This worked for me:
#FXML
public void autoShow() {
checkmenuitem.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
e.consume();
}
});
}
checkmenuitem is the id. When it is clicked, the handle method is executed. For a detailed explanation on the event.consume() method, see this:
What is the meaning of Event consumes in JavaFX
Place the above method in your controller class and then call it from the initialize method in your controller class:
#Override
public void initialize(URL location, ResourceBundle resources) {
autoShow();
}

Categories