Why is my accessibility service not being started? - java

I have created an accessibility service to see if I can perform gesture navigation with my app. The problem is my accessibility service is not being started at all. A possible hint into this problem is that I do not see my app in the accessibility portion of the settings page. I have added the proper permission in my Manifest file, and do not see where I am going wrong.
Here is my Manifest file:
<service
android:name=".AccessibilityService"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="#string/accessibility_service_label">
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/accessibility_service_config" />
</service>
Here is my accessibility service configuration XML file:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRequestEnhancedWebAccessibility="true"
android:canRetrieveWindowContent="true"
android:packageNames="com.example.adtry3"
android:canRequestTouchExplorationMode="true"
android:settingsActivity="com.example.adtry3.MainActivity" />
Here is my accessibility service class:
package com.example.adtry3;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.annotation.TargetApi;
import android.os.Build;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import androidx.annotation.RequiresApi;
import java.util.List;
#TargetApi(Build.VERSION_CODES.DONUT)
public class AccessibilityService extends android.accessibilityservice.AccessibilityService {
#Override
public void onServiceConnected(){
System.out.println("ONSERVICECONNECTED");
AccessibilityServiceInfo info = getServiceInfo();
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
info.packageNames = new String[] {"com.example.adtry3"};
info.notificationTimeout = 100;
setServiceInfo(info);
}
#RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
System.out.println("Testing");
}
#Override
public void onInterrupt() {
}
public void onUnbind(){ //service is being terminated, do final one-time operations here
}
}
Lastly, this is the OnCreate method of my MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivityForResult(intent, 0);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
If I am missing something, or if you know what is going on, please let me know. I have search on other StackOverflow answers and have had no luck. Thank you.

add this filter to your <service declaration in manifest
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
also you may have to add these lines in your service config xml
android:description="#string/accessibility_service_desc"
android:notificationTimeout="25"

Related

bindService android AIDL always return false

I would like to provide a service that can be called by other app. Therefore, I have a service and an aidl. But when I try to have a separate application to bind this service (bindService), it just returns me false which means fail. Here is my code.
I'm using the code from books Pro Android 2
I've already tried many solutions in other questions similar to this, but there isn't any working solution for me.
I've tried fix the aidl intent filter but it's still not working
I've tried fix the package name and class name in client app, but it's still not working.
Please help me!
On Client :
// IStockQuoteService.aidl
package id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain.stockquoteservice;
// Declare any non-default types here with import statements
interface IStockQuoteService {
double getQuote(String ticker);
}
package id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain.stockquoteclient;
import id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain.stockquoteservice.IStockQuoteService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final String TAG = "StockQuoteClient";
private IStockQuoteService stockService = null;
private Button bindBtn;
private Button callBtn;
private Button unbindBtn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindBtn = (Button)findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClassName("com.id.ac.ui.cs.mobileprogramming" +
".ahmad_fauzan_amirul_isnain.stockquoteservice",
"com.id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain" +
".stockquoteservice.IStockQuoteService");
Log.d("Hasil", String.valueOf(bindService(intent,
serConn, Context.BIND_AUTO_CREATE)));
bindBtn.setEnabled(false);
callBtn.setEnabled(true);
unbindBtn.setEnabled(true);
}});
callBtn = (Button)findViewById(R.id.callBtn);
callBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
callService();
}});
callBtn.setEnabled(false);
unbindBtn = (Button)findViewById(R.id.unbindBtn);
unbindBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
unbindService(serConn);
bindBtn.setEnabled(true);
callBtn.setEnabled(false);
unbindBtn.setEnabled(false);
}});
unbindBtn.setEnabled(false);
}
private void callService() {
try {
double val = stockService.getQuote("SYH");
Toast.makeText(MainActivity.this, "Value from service is "+val,
Toast.LENGTH_SHORT).show();
} catch (RemoteException ee) {
Log.e("MainActivity", ee.getMessage(), ee);
}
}
private ServiceConnection serConn = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service)
{
Log.v(TAG, "onServiceConnected() called");
stockService = IStockQuoteService.Stub.asInterface(service);
callService();
}
#Override
public void onServiceDisconnected(ComponentName name) {
Log.v(TAG, "onServiceDisconnected() called");
stockService = null;
}
};
}
On Service :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain.stockquoteservice">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="StockQuoteService">
<intent-filter>
<action android:name="com.id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain.stockquoteservice.IStockQuoteService"
/>
</intent-filter>
</service>
</application>
</manifest>
package id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain.stockquoteservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class StockQuoteService extends Service
{
private static final String TAG = "StockQuoteService";
public class StockQuoteServiceImpl extends IStockQuoteService.Stub
{
#Override
public double getQuote(String ticker) throws RemoteException
{
Log.v(TAG, "getQuote() called for " + ticker);
return 20.0;
}
}
#Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate() called");
}
#Override
public void onDestroy()
{
super.onDestroy();
Log.v(TAG, "onDestroy() called");
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.v(TAG, "onStart() called");
}
#Override
public IBinder onBind(Intent intent)
{
Log.v(TAG, "onBind() called");
return new StockQuoteServiceImpl();
}
}
// IStockQuoteService.aidl
package id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain.stockquoteservice;
// Declare any non-default types here with import statements
interface IStockQuoteService {
double getQuote(String ticker);
}
I'm using the code from books Pro Android 2
That book is from 2010. Much of that book will be out of date. Please use something newer. If nothing else, you can download older editions of one of my books for free. Right now, the most recent version of those is from 2015 — while that too is a bit old, it is much more up to date than a book from 2010.
I've already tried many solutions in other questions similar to this, but there isn't any working solution for me.
You have:
intent.setClassName("com.id.ac.ui.cs.mobileprogramming" +
".ahmad_fauzan_amirul_isnain.stockquoteservice",
"com.id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain" +
".stockquoteservice.IStockQuoteService");
The package in your manifest has id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain.stockquoteservice. Assuming that your applicationId in your module's build.gradle file is the same, I think that this matches what you have in your code. Your application ID is very long — in the future, I recommend that you use something shorter and easier to visually compare.
However, your class name is wrong. Your <service> class is StockQuoteService, not IStockQuoteService. IStockQuoteService is the name of the AIDL, not the service, and your Intent needs to point to the service. So, try:
intent.setClassName("com.id.ac.ui.cs.mobileprogramming" +
".ahmad_fauzan_amirul_isnain.stockquoteservice",
"com.id.ac.ui.cs.mobileprogramming.ahmad_fauzan_amirul_isnain" +
".stockquoteservice.StockQuoteService");
Or, to reduce duplication:
String packageName = "com.id.ac.ui.cs.mobileprogramming" +
".ahmad_fauzan_amirul_isnain.stockquoteservice";
intent.setClassName(packageName, packageName+".StockQuoteService");

Why isn't my BroadcastReceiver starting on the Android Emulator?

I'm new to Android development, and I'm trying to create a simple "proof of concept application" which will run as a background service. I'm trying to use IntentService with a BroadcastReceiver to kick off the process (during boot time for now, at some point I might switch it to Screen on / user present).
I created a new project within Android Studio with no activity. Then I added the following Java files and made the following changes to the AndroidManifest.xml.
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.circlesquires.netcountable.netcountable">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">
</uses-permission>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name=".SnapshotService" android:exported="true">
</service>
<receiver android:name=".ServiceStarter" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
ServiceStarter.java
package com.circlesquires.netcountable.netcountable;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by camha on 6/18/2016.
*/
public class ServiceStarter extends BroadcastReceiver{
static final String ACTION = "android.intent.action.BOOT_COMPLETED";
#Override
public void onReceive(Context context, Intent intent) {
Log.i("output", "onReceive occured!");
if(intent.getAction().equals(ACTION)) {
Intent serviceIntent = new Intent(context, SnapshotService.class);
context.startService(serviceIntent);
}
}
}
SnapshotService.java
package com.circlesquires.netcountable.netcountable;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
/**
* Created by camha on 6/18/2016.
*/
public class SnapshotService extends IntentService {
public SnapshotService() {
super("SnapshotService");
}
#Override
protected void onHandleIntent(Intent workIntent) {
while(true) {
Log.i("output", "I'm running!");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
I deploy the application to an emulator being sure to click the "debug" button. However I don't ever see ANY of the logs outputted in logcat.
I'm sure I'm doing something wrong--help me figure out what :D. Thanks!
android.intent.action.BOOT_COMPLETED is send, as its name suggest, after, but only after a device boot time. Are you restarting your device?
Instead of restarting device to test out your application, you can send system broadcasts directly through adb:
./adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n <your.package.name>/.<YourReceiverClass>
You can find more info here.
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent(context, SnapshotService.class);
context.startService(i);
}
compare direct with intent value instead of making string
and one thing more
On Android 3.1 and higher, the user must launch one of your activities before any manifest-registered BroadcastReceiver will work.
check this https://stackoverflow.com/a/17067671/3288890

How to set an image to the button using the other application package name?

Im facing a problem as below stated.
I'm having a single button on the widget.On click of the button it open other application.Now my question is how to assign that open application image to my button which is in the widget.
Please help me.
This is my Main class.
{
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
public class WidgetMainProvider extends AppWidgetProvider {
// Activity a=new Activity();
public static String GET_ACTION = "Get_Action";
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (GET_ACTION.equals(intent.getAction())) {
Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
}
}
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
//Which defines the UI of Widget
// initializing widget layout
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.widget1);
Intent intent = new Intent(context, WidgetOneActivity.class);
// intent.setAction(GET_ACTION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.button,pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}}}
This is my another class where i have declared the package name,based on that it will open the application.
package com.purvotara.example.widget;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class WidgetOneActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PackageInfo pkgInfo=new PackageInfo();
context.getPackageManager().getApplicationIcon(pkgInfo.packageName);
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.drdo.s.a.m.s");
startActivity(LaunchIntent);
}
}
And also please help me where to put the code which which will set image to the button dynamically.
Here is my Android. manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.eightbitcloud.example.widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name="com.purvotara.example.widget.WidgetOneActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.purvotara.example.widget.WidgetMainProvider"
android:label="#string/widget1name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#XML/widget1_info" />
</receiver>
</application>
</manifest>
Please help me geeks,im waiting.
As I understand, you are trying to assign another app icon to your widget button. You can try:
PackageManager packageManager = mContext.getPackageManager();
Drawable icon = packageManager .getApplicationIcon("com.example.yourapp");
button.setBackgroundDrawable(icon);
or
Drawable icon = getPackageManager().getApplicationIcon("com.example.yourapp");
button.setBackgroundDrawable(icon);
I added the following in the WidgetOneActivity class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = null;
//YOU NEED TO INITIALIZE YOUR BUTTON HERE, YOUR BUTTON IS NULL
try {
Drawable icon;
icon = getPackageManager().getApplicationIcon("com.drdo.s.a.m.s");
button.setBackground(icon);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.drdo.s.a.m.s");
startActivity(LaunchIntent);
}
});
}
After adding this my app is closing.

PreferenceActivity and BroadcastReceiver - Implement dynamic preferences

I'm going by the following code example to dynamically build a preference activity.
http://www.linuxtopia.org/online_books/android/devguide/guide/samples/ApiDemos/src/com/example/android/apis/app/PreferencesFromCode.html
The preference dialog shows, but I'm not able to see any changes after closing it.
Here's where I'm defining the activity in AndroidManifest.xml
<activity
android:name="PreferencesActivity" android:label="#string/preferences_name">
</activity>
Here's where I'm defining the receiver.
<receiver
android:name="FroyVisualReceiver"
android:label="#string/app_name"
android:exported="false">
<intent-filter>
<action android:name="com.starlon.froyvisuals.PREFS_UPDATE"/>
</intent-filter>
</receiver>
And here's the BroadcastReceiver. I never see the "WTF" in logcat. What am I doing wrong?
package com.starlon.froyvisuals;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
public class FroyVisualsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("WTFWTF", "WTFWTFWTFW");
String action = intent.getAction();
if(action.equals("com.starlon.froyvisuals.PREFS_UPDATE"))
{
((FroyVisuals)context).updatePrefs();
}
}
}
Oh here's onPause where I'm broadcasting the PREFS_UPDATE intent.I do see the logcat message. This method is part of my PreferenceActivity.
/** another activity comes over this activity */
#Override
public void onPause()
{
Log.i(TAG, "onPause ================================ ");
super.onPause();
Intent i = new Intent(this, FroyVisualsReceiver.class);
i.setAction("com.starlon.froyvisuals.PREFS_UPDATE");
sendBroadcast(i);
}
Edit: I think it may have to do with this line. 'this' points to my PreferenceActivity.
Intent i = new Intent(this, FroyVisualsReceiver.class);
Try a simple Intent:
Intent i = new Intent();
i.setAction("com.starlon.froyvisuals.PREFS_UPDATE");
sendBroadcast(i);

Activity wont start with "startActivity();"

Here's my code for DroidArmoryActivity
package com.maxgenero.droidarmory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class DroidArmoryActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.ibM4A1:
Intent intentM4A1 = new Intent("com.maxgenero.droidarmory.M4A1GUN");
startActivity(intentM4A1);
break;
}
}
}
It's not starting the java file (Activity) at all, no errors. Btw, the case is looking for an imageButton.
Here's my Manifest, at least the part you need:
<activity android:name=".M4a1"
android:label="#string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="com.maxgenero.droidarmory.M4A1" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
And the file name for the java file is M4a1.java. If you need more info let me know, thanks.
I dont see where you define a listener on your Button or your View that will be clicked to launch the second Activity ??
yourView.setOnClickListener(this);
the second thing is that you should add declare your activity on your manifest file on the tag like this :
<activity android:name="your.package.name.NameOfYourAcitivity" />
the last thing is : try to instantiate the intent like this :
this.startActivity(new Intent(this, SecondActivity.class));
Regards,
Instead of...
case R.id.ibM4A1:
Intent intentM4A1 = new Intent("com.maxgenero.droidarmory.M4A1GUN");
startActivity(intentM4A1);
Try
Intent intentM4A1 = new Intent(this, ACTIVITY_NAME.class);
startActivity(intentM4A1);
Also dont forget to call your setOnclickListener().

Categories

Resources