Sending Scroll event to another application - java

I think is question is bit crazy, I basically want to send scroll event from my app to another app ( eg chrome ). So basically if chrome is opened and when user press vol up or down button, my background activity listen for that button event and if its a up event then it will scroll chrome opened webpage up or down. I am not sure is that possible because I was reading through some doc's from google and its all saying we need root permission to sent event from one app to another. I don't have any code yet. So any one know this is possible with android api ?
Thanks

I cannot answer in the context of chrome. However, You can essentially serialize what you need and send it via the intent system to another application.
As an example, in your first application, you receive the touch event
boolean onTouch(MotionEvent event){
if(event.getActionMasked() == MotionEvent.ACTION_UP){
Intent startOtherApplication = new Intent();
startOtherApplication.putExtra("ACTION_UP_X",event.getX());
startOtherApplication.putExtra("ACTION_UP_Y",event.getY());
startOtherApplication.setComponent(new ComponentName("com.your.other.application", "com.your.other.application.ActivityYouWantToHandleTouch"));
startActivity(intent);
}
}
You would then extract the intent in the Activity (ActivityYouWantToHandleTouch) in your other Application.
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent fetchedIntent = getIntent();
boolean hasUpAction = false;
int x = 0;
int y = 0;
if(fetchedIntent != null){
hasUpAction = (fetchedIntent.getExtra("ACTION_UP_X")!=null);
x = fetchedIntent.getExtra("ACTION_UP_X");
y = fetchedIntent.getExtra("ACTION_UP_Y");
}
}

Related

How to pass a parameter out of an onClick

New to Android Studio. I'm creating an app project for practice and I am trying to create a Menu Activity. I want to test to see if I can mute sounds and hide the display of text (score) via a Menu UI. I get that I can use Intent to pass values back and forth between activities and that I can use those values to turn features on and off across the app.
I cannot figure out with a button and onClick how to get a variable to change so that I can pass it via Intent. I've only seen it done INSIDE the onClick. I'm trying to change the variable OUTSIDE the onClick.
Example Code:
public class Menu extends AppCompatActivity {
private boolean soundOn = true;
private Button isSoundOn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
isSoundOn = findViewById(R.id.isSoundOn_button);
isSoundOn.setOnClickListener(v -> {
soundOn = false;
});
Now when I attempt to access the soundOn parameter and pass it on via Intent to another activity the value remains true, it never changes after the button is clicked.
I did figure out one trick, I can use intent and pass the value to the same activity, like so:
soundOff.setOnClickListener(v -> {
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", false);
startActivity(intent);
This reloads the Activity after the button is clicked, it makes it appear as though a glitch happened as it is run, but I think that issue could be resolved via altering the transition animation...I think. However, this seems like a clumsy approach, especially in a Menu Activity that could have numerous settings to edit (sound, score, language, timer, color, background, etc.).
I get that I can have the onClick go back to the original Activity with the change, but I want to create a menu where I can have multiple selections made and then pass them all back to the original Activity.
I hope this makes sense, I know this is rather basic, but I'm new to this and my searching hasn't been able to yield a solution. Thanks.
If you are doing an intent to the same Activity you should retreive your intent on the onCreate method:
isSoundOn = intent.getBooleanExtra("soundOn", true) //true is the default parameter in case intent does not contain that key;
That way you are always checking your intent in case you need it.
You also need to use your variable in the intent; right now you are always setting it to false:
soundOff.setOnClickListener(v -> {
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", soundOn);
startActivity(intent);
}
There are other solutions, for example: you can use SharedPreferences for persisting your values and then call this.recreate to recreate Activity again and avoid using intents. Then you can retreive your SharedPreferencesvalues on the onCreate method to do whatever you want.
Now when I attempt to access the soundOn parameter and pass it on via
Intent to another activity the value remains true, it never changes
after the button is clicked.
Lets start with keeping track of soundOn
When Menu activity is first launched soundOn = true
When isSound button is clicked soundOn = false
Intent intent = new Intent(Menu.this, Menu.class);
intent.putExtra("soundOn", soundOn); // false is the value that is extra.
startActivity(intent);
When MenuActivity is again launched due to the intent soundOn = true this is because of this line
private boolean soundOn = true; //<---
You are passing Extra in intent but you arent storing the intents extra value in soundOn
Thats why it is true always
to solve this use need to Get the Intent Extra that you have passed and we do it in onCreate method
private boolean soundOn = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Intent intent = getIntent();
if(intent != null) { // we need to get this cause when we first start our app we dont start it with any intents
soundOn = intent.getExtra("soundOn");
}
}

One button goes to different activities

I want to make a one button goes to different activities, I have uploaded an image to show how I planned to do.
The idea is to come from Activity 1 or Activity 2 and find the same Layout and Button and go through it to Activity 7 or Activity 8 according to the Activity I was on.
I hope that I have explained my idea with a good way to be understood.
You need to pass an identifier or key with intent from your 1st part of activities as shown in the image and based on that key identify where the activity came from and where is it supposed to go. You can use if statements inside your button click function
Example
in xml for all your 3 button
<Button
android:name = "#id/button1"
android:onClick = "button1">
define other button and their properties
in java file
public void button1(View v)
{
Intent i = getIntent()
String key = i.getStringExtra("key");
if(key == "activity5")
{
Intent i = new Intent(this, Activity5.class);
startActivity(i);
}
else if (key == "activity6")
{
Intent i = new Intent(this, Activity6.class);
startActivity(i);
}
}

how to open a webpage, fill an html form, submit it, get the form's result and then post the information to another activity in android

We have an aspx page with a form in it and we can only process this form in a browser-based environment (ie we cannot create a web service for it.
What we want to do is that we want to process this information in an android application. We want to send some initial data to this page, let the user fill it, submit the form, and then return the results to the calling activity, so we can show a proper message and act accordingly.
let me give an example for this usecase:
We are in activity A and in this activity we have a button that triggers the start of our journey:
//we are in activity A
mOurButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("additional_data_we_need", adittionalData);
startActivityForResult(intent, REQUEST_CODE);
}
});
Now we are in activity B and here, we should comunicate with that webpage that I was talking about, passing it the additionalData that is in this activity's extra bundle (it is acceptable if we do it using query strings or any other way).
After the user fills the form, and hit submit,we do some operation on it and return some results to another (web)page or in the current (web)page's postback (again it is acceptable if we use query strings, post method or any other way; whicever way that works for this senario). We get this returned result in android side and then get the result to ActivityA :
//we are in acitivity B
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
int adittionalData = getIntent().getExtras().getIntExtra("adittional_data_we_nedd");
//this is the first place that I don't know what to do
callTheWebPageAndGiveItTheData(adittionalData);
}
public void SomeListenerOfTheReturnedDataFromTheWebpage(Bundle returnedData //doesn't necessarily need to be of bundle type) {
intent intent = new Intent();
intent.putExtra("return_bundle", returnedData);
setResult(Activity.RESULT_OK, intent);
finish();
}
What can I do to achieve this goal?
Thanks in advance for your kind answers

Determine whether onPause() was fired by user navigation or by my activity launching another one

How can I determine whether onPause() was fired because my activity launched a new one (e.g. a photo picker intent) or because the user navigated away from my activity (e.g. by pressing home)?
A simple solution is to have a state variable in the Activity with some fixed values:
final static int RUNNING = 0;
final static int CALLED_SOMETHING = 1;
int state = RUNNING:
Then, whenever you launch an activity:
state = CALLED_SOMETHING;
And when it returns:
state = RUNNING;
And in your onPause():
switch(state) {
case RUNNING:
// Do the stuff if Home etc were pressed
break;
case CALLED_SOMETHING:
// Do the other stuff
break;
}
Obviously, you can extend this to further scenarios. You may also wish to catch onBackPressed() to make sure you handle the situation with as much understanding as possible.

Android popup style activity which sits on top of any other apps

What I want to create is a popup style application.
I have a service in the background - something arrives on the queue and i want an activity to start to inform the user - very very similar to the functionality of SMSPopup app.
So I have the code where something arrives on the queue and it calls my activity.
However for some reason the activity always shows on top of the originally started activity instead of just appearing on the main desktop of the android device.
As an example:
I have the main activity which is shown when the application is run
I have the service which checks queue
I have a popup activity.
When i start the main activity it starts the service - I can now close this.
I then have something on the queue and it creates the popup activity which launches the main activity with the popup on top of it :S How do I stop this and have it behave as i want...
The popup class is :
public class SMSPopup extends Activity implements OnClickListener{
public static String msg;
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
// Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
this.setContentView(R.layout.popup);
TextView tv = (TextView)findViewById(R.id.txtLbl);
Intent intent = getIntent();
if (intent != null){
Bundle bb = intent.getExtras();
if (bb != null){
msg = bb.getString("com.andy.tabletsms.message");
}
}
if(msg == null){
msg = "LOLOLOL";
}
tv.setText(msg);
Button b = (Button)findViewById(R.id.closeBtn);
b.setOnClickListener(this);
}
#Override
public void onClick(View v) {
this.finish();
}
}
and I call the activity from a broadcast receiver which checks the queue every 30 seconds or so :
if(main.msgs.size()>0){
Intent testActivityIntent = new Intent(context.getApplicationContext(), com.andy.tabletsms.work.SMSPopup.class);
testActivityIntent.putExtra("com.andy.tabletsms.message", main.msgs.get(0));
testActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(testActivityIntent);
}
The layout is here : http://pastebin.com/F25u6wdM
This is against the design practice suggested by Android. See http://developer.android.com/guide/topics/ui/notifiers/notifications.html
A background Service should never launch an Activity on its own in order to receive user interaction.
You could show the message in a Toast and/or notification. From the notification, you could start a new intent.

Categories

Resources