Android return value from AccessibilityService - java

i have this AccessibilityService class for the USSD
public class USSDService extends AccessibilityService {
public static String TAG = "USSDService";
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG, "onAccessibilityEvent");
String text = event.getText().toString();
if (event.getClassName().equals("android.app.AlertDialog")) {
performGlobalAction(GLOBAL_ACTION_BACK);
Log.d(TAG, text);
Intent intent = new Intent("com.times.ussd.action.REFRESH");
intent.putExtra("message", text);//this is the value that i want to return to MainActivity
Toast.makeText (this,text,Toast.LENGTH_LONG).show();
}
}
#Override
public void onInterrupt() {
}
#Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.d(TAG, "onServiceConnected");
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.flags = AccessibilityServiceInfo.DEFAULT;
info.packageNames = new String[]{"com.android.phone"};
info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
setServiceInfo(info);
}}
and i invoke in main activity like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
////////////////USSD////////////////////
Intent mIntent = new Intent(this, USSDService.class);
startService(mIntent);
}
and this is a function that dial ussd
private void dailNumber(String code) {
String ussdCode = "*" + code + Uri.encode("#");
Intent callIntent = new Intent(Intent.ACTION_CALL)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setData(Uri.parse("tel:" + ussdCode));
callIntent.putExtra("simSlot", 0);
startActivity(callIntent);
}
when i call this function by clicking a button, every thing is ok and the response of ussd call show in Toast as i code it in function onAccessibilityEvent
what i want is return the response value to MainActivity

Accessibility services cannot be started in the manner you are attempting to start them. They can only be managed from the Accessibility Settings menu. This is for good reason. Accessibility services can read screen contents. OH, you downloaded my app, that's nice. NOW I'm going to launch an Accessibility Service that watches all of your screen content for things that look like Credit Card numbers.
Intent mIntent = new Intent(this, USSDService.class);
startService(mIntent);
The lines of code above ARE NOT accomplishing what you think they are. The accessibility service will launch (call your service's onCreate), but it will launch in a functionally limited state that does not allow it to bind to applications and the Android Operating System in the way an Accessibility Service normally would.
Once properly configured, go to Settings -> Accessibility and find your service listed next to TalkBack. Then turn it on. Once you do this, in order to communicate between your two processes you're going to need some type of interprocess communication mechanism. For simple communication you can consider using SharePreferences. Otherwise, you're probably going to have to look into BroadCastReceivers.
See how far that gets you, and ask another question if you need!

Related

How do I determine if setPersistenceEnabled is already enabled?

All works well. Once the app is running, and I press home and back to the app through the multitask viewer, it works well. But once it is running, and I press it's icon from the drawer, it crashes because it is calling again "setPersistenceEnabled()" when it is already running. So, how can I check if it is enabled before trying to enable it? My code:
public class SplashActivity extends AppCompatActivity {
private FirebaseUser firAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getInstance().setPersistenceEnabled(true);
firAuth = FirebaseAuth.getInstance().getCurrentUser();
if (firAuth!=null) {
// User is signed in.
Intent intent = new Intent(this, Identificador.class);
startActivity(intent);
finish();
} else {
// No user is signed in.
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
};
}
I recommend you using the following lines of code:
private static boolean calledAlready = false;
if (!calledAlready) {
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
calledAlready = true;
}
Run your app in debug mode. Set a breakpoint at your SplashActivity onCreate() an run through your use case that crashes the app. You'll see that SplashActivity onCreate() is invoked every time you launch your app after you leave it, because that's how activities work when they're exited and re-entered.
If you want to prevent repeated calls to setPersistenceEnabled(), you'll have to guard them against the invocation of your launch activity's onCreate() by setting some global variable to indicate that it's already been called, or put the call in a ContentProvider that gets created only once per process. I suggest the latter.

Recurring background task Android - Handler vs AlarmManager

I am working on an application for research purposes. We want to track some user activity on the phone: if some actions are easily detactable with broadcast receivers, we want also to check current running applications.
This application works only on Android devices that runs android 5.0 or lower.
My problem is that I post a Runnable Obj in a handler, Runnable posts again itself in handler after HALF_SECOND (see code for details). In runnable I get information and send them to IntentService to perform work.
Everything works fine: app starts at boot, handler and runnable do their job in background UNLESS I open the main Activity.
The app is able to keep going for days, but if I open the main Activity and then close it from "recent open activities" with a swipe, or from the memory task manager, handler and runnable stop, even if they are not called/accessed by the activity (they are in a Separate Service).
Moreover, not always a call to onDestroy (of the activity or Service) is made.
Reading online I understand that swipe or task manager remove the app from memory abrouptly thus not always calling onDestory.
What I want to achive is to make the handler start again soon after the main activity is closed.
What I have tried is to put some check in onPause method of the activity, making sure to remove this check if onStart is called again (like in case the the app switches from vertical to horizontal layout, or if home button is pressed and then app is opend again). Also implemented a way to make the handler send "ImAlive" intent to a broadcast receiver, which should restart the service that starts the handler, if intents do not arrive before a count down is finished. Unfortunately, as soon the main activty stops existing, even the broadcast is automatically unregistered and destroyed.
My question is, is there a way to create something that is able to make my handler restart if the activity is closed? Or is there some other pattern that can help me as workaround for what I want to achieve? Because I am polling data every half second I read is better to use handler, because Timer augments small interval to a greater interval, and AlarmManager is not precise enough for very small interval.
What I want to achieve is something similar to Facebook, Instagram, Whatsapp, Telegram app, that are always in memory, and even if you force to terminate them, after a few seconds are back again there... how?
We are not interested in battery issues because of continuous polling to data. As for research purposes we don't mind if the phone on which we are testing last 2 days straight, 1 day or 12 hours or less.
Here the code: OnBootService is started from broadcast receiver, declared in manifest when onBootCompleted and ShutDown actions are received, in order to start and stop handler.
public class OnBootService extends Service{
private static final Handler handler = new Handler();
private final long HALF_SEC = 500;
private RunnableTest r = null;
private Context myContext = this;
private final String TAG = "BootService";
// Extras
public static final String START = "start";
public static final String STOP = "stop";
#Override
public IBinder onBind(Intent intent){
return null;
}
#Override
public int onStartCommand(Intent intent, int flag, int startId){
String action = intent.getAction();
switch(action){
case START: startHandler();
break;
case STOP: stopHandler();
break;
}
return START_NOT_STICKY;
}
private void startHandler(){
if(r == null){
r = new RunnableTest();
handler.post(r);
Log.i(TAG, "----Handler started!");
}
}
private void stopHandler(){
if(r != null){
Log.i(TAG, "----calling STOP");
handler.removeCallbacks(r);
r = null;
}
}
private class RunnableTest implements Runnable {
private String TAG = "RunnableTest";
public RunnableTest(){}
#Override
public void run(){
handler.removeCallbacks(this);
// Do stuff
Intent i = new Intent(myContext, MyIntentService.class);
i.putExtra("addStuff", myStuff);
myContext.startService(i);
handler.postDelayed(this, HALF_SEC);
}
}
Activity is empty: all method overridden just to understand proper Activity lifecycle, but else is empty for now.
public class MainActivity extends AppCompatActivity {
private final String TAG = "Activity";
private Context myContext = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// access a file and get stored information to show
setContentView(R.layout.activity_main);
Toast.makeText(getBaseContext(), "Application open successfully", Toast.LENGTH_LONG).show();
}
#Override
protected void onRestart(){
super.onRestart();
Log.e(TAG, "----onRestart Called");
}
#Override
protected void onStart(){
super.onStart();
Log.e(TAG, "----onSTART Called");
}
#Override
protected void onResume(){
super.onResume();
Log.e(TAG, "----onRESUME Called");
}
#Override
protected void onPause(){
super.onPause();
Log.e(TAG, "----onPAUSE Called");
}
#Override
protected void onStop(){
super.onStop();
Log.e(TAG, "----onSTOP Called");
}
#Override
protected void onDestroy(){
super.onDestroy();
Log.e(TAG, "----onDestroy Called");
}
}
Any help is really appreciated, if you need some more information on the code, I will update the post.
Thank you!
Android system can restart the service if u return START_STICKY inside onStartCommand().
It works perfectly on all lower version than Lollipop in Android.
No need of CountDownTimer.

how to get available network list in android?

Hi I am beginner to Android.I want get available network list using com.android.phone.NetworkSetting.For that, I tried to get the network list by call this package, when I click a scan button.The output is automatically call the android core Network Settings.Its Start automatically search the available networks and list outed available networks, Instead of I want get the same available network list in my List View.So please guide me to retrieve the network name and details from this package.
scanbtnOnclick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClassName("com.android.phone", "com.android.internal.phone");
intent.setClassName("com.android.phone", "com.android.phone.NetworkSetting");
startActivity(intent);
}
});
I also accept another method to retrieve this same concept.
use wifimanager API to manage all the wi-fi connections.
WifiManager mainWifiObj;
mainWifiObj = (WifiManager) getSystemService(Context.WIFI_SERVICE);`
public class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
}
}
WifiScanReceiver wifiReciever = new WifiScanReceiver();
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
You can put scan results into string variable and use it.
List<ScanResult> wifiScanList = mainWifiObj.getScanResults();
String data = wifiScanList.get(0).toString();
hope this will be helpful to you.

Android: how do I tell my activity to start new activity from my service?

I have a service that is, among other things, downloading images from internet. When this is done I want to show this image in a custom Activity that has a dialog theme. But I only want to use this pop up if the app is running, otherwise I just want to create a notification.
But I get an exception when I try to start an activity from my service and i feel that maybe this isn't the right way to do it?
It says:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
So my question is if this is the right way to do this by setting that flag or how should I get my downloaded image from my service to an activity. Can I in some way tell an activity to start a new activity from my service class?
I think using Broadcast Receiver is better option for you.
Add Below Method in Service and call this method when image Download Complete.
private void updateMyActivity(Context context) {
if(MainActivity.activityStatusFlag){
//update the activity if activityStatusFlag=true;
Intent intent = new Intent("mUpdateActivity");
context.sendBroadcast(intent);
}else{
//display notification if activityStatusFlag=false;
}
}
In Activity Add Following Code.
public class MainActivity extends Activity{
public static boolean activityStatusFlag= false;
//define this variable to check if activity is running or not.
#Override
protected void onResume() {
super.onResume();
activityStatusFlag = true;
this.getApplicationContext().
registerReceiver(mMessageReceiver,new IntentFilter("mUpdateActivity"));
}
#Override
protected void onPause() {
super.onPause();
activityStatusFlag = false;
this.getApplicationContext().unregisterReceiver(mMessageReceiver);
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Display Popup or update Activity
}
};
}

Opening Android system apps/utilities

I am learning Android. I learned to use my app to start another app I wrote. Using this code below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button appOpener = (Button) findViewById(R.id.button1);
appOpener.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager firstApp = getPackageManager();
i = firstApp.getLaunchIntentForPackage("com.assignment.projecttomiko");
}
});
}
It's pretty straight-forward and I am happy wit it. But now I want to start apps already on the Android platform itself, like browser, calender, calculator, etc. From the above, I see I just have to put in the package name of my other apps. I am assuming I can do the same thing to say start a calculator. Or maybe not. What is the best way to do this? If I can use package names, then what are the package names of these in-built apps. If there's a better way I would like someone to help me out.
I read this: Opening System Application Using Intent and realised there's an introduction of an extra LaunchComponent I don't know what that is but the main thing is I see on that thread that they are also putting in some package names to call system apps. What'll be your best way to get this done? :)
EDIT:
I searched for hours and tried this code but doesn't work :(
// activity name and package for stock calculator
private static final String CALCULATOR_PACKAGE_NAME = "com.android.calculator2";
private static final String CALCULATOR_CLASS_NAME = "com.android.calculator2.Calculator";
public void launchCalculator() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(CALCULATOR_PACKAGE_NAME,
CALCULATOR_CLASS_NAME));
try {
this.startActivity(intent);
} catch (ActivityNotFoundException noSuchActivity) {
// handle exception where calculator intent filter is not registered
}
}

Categories