I'm trying to use fingerprint gestures in an accessibility service. It is working fine in android 8.1 devices (Nexus 5x emulator, Moto G5s Plus) but not working in android 9 devices (Nexus 5x emulator, Samsung M30s). I've added all the required lines mentioned in this question:
Android O - fingerprint gesture callback not working
Do I have to add something extra for android 9? Can someone please help me?
activity_service.xml:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackGeneric"
android:canRequestFingerprintGestures="true"
android:accessibilityFlags="flagDefault|flagRequestFingerprintGestures"
android:canPerformGestures="true"/>
MyAccessibilityService.java:
public class MyAccessibilityService extends AccessibilityService {
private static final String TAG = MyAccessibilityService.class.getSimpleName();
#Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
Log.d(TAG, "onAccessibilityEvent");
}
#Override
public void onInterrupt() {
Log.d(TAG, "onInterrupt");
}
#Override
protected boolean onGesture(int gestureId) {
Log.d(TAG, "onGesture " + gestureId);
return super.onGesture(gestureId);
}
#Override
protected boolean onKeyEvent(KeyEvent event) {
Log.d(TAG, "onKeyEvent " + event.getKeyCode());
return super.onKeyEvent(event);
}
#Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "onDestroy" , Toast.LENGTH_SHORT).show();
super.onDestroy();
}
#Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.d(TAG, "onServiceConnected");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
FingerprintGestureController gestureController = getFingerprintGestureController();
Toast.makeText(getApplicationContext(), "Is available: " + gestureController.isGestureDetectionAvailable(), Toast.LENGTH_LONG).show();
Log.e(TAG, "Is available: " + gestureController.isGestureDetectionAvailable() );
FingerprintGestureController.FingerprintGestureCallback callback = new
FingerprintGestureController.FingerprintGestureCallback() {
#Override
public void onGestureDetectionAvailabilityChanged(boolean available) {
super.onGestureDetectionAvailabilityChanged(available);
Toast.makeText(getApplicationContext(), "Gesture available change to: " + available, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onGestureDetectionAvailabilityChanged " + available);
}
#Override
public void onGestureDetected(int gesture) {
super.onGestureDetected(gesture);
switch (gesture){
case GESTURE_SWIPE_DOWN:
CameraManager mCameraManager;
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String mCameraId = "";
try {
mCameraId = mCameraManager.getCameraIdList()[0];
mCameraManager.setTorchMode(mCameraId, true);
}
catch (Exception e){
}
break;
case GESTURE_SWIPE_UP:
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
mCameraId = mCameraManager.getCameraIdList()[0];
mCameraManager.setTorchMode(mCameraId, false);
}
catch (Exception e){
}
break;
default:
Toast.makeText(getApplicationContext()
, "Gesture: " + gesture, Toast.LENGTH_SHORT).show();
}
Log.d(TAG, "onGestureDetected " + gesture);
}
};
gestureController.registerFingerprintGestureCallback(callback, new Handler());
}
}
#Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind " );
return super.onUnbind(intent);
}}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.bignerdranch.com.myapplication">
<uses-feature android:name="android.hardware.fingerprint"/>
<uses-feature android:name="android.hardware.camera.flash" />
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
<uses-permission android:name="android.permission." />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" />
<application>
<service
android:name=".MyAccessibilityService"
android:label="#string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/activity_service" />
</service>
</application>
</manifest>
You need to add this line in the gesture service xml
android:accessibilityFlags="flagDefault|flagRequestFingerprintGestures"
It could also be device related. Check the following answer for information.
Android M FingerprintManager.isHardwareDetected() returns false on a Samsung Galaxy S5
Can you provide your code samples for further debugging if this doesn't work?
Related
I am trying to detect gestures on a fingerprint scanner. I have made an Accessibility service and yet I get back "False" for this method isGestureDetectionAvailable(). However, my device is capable of detecting the gestures.
I have applied everything according to the code mentioned on the official Android developer website.
This is my Android Manifest Code.
<application
.....>
<service
android:name=".AccessibilityService"
android:enabled="true"
android:exported="true"
android:label="My application"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action
android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/my_gesture_service" />
</service>
This is my Service.
public class AccessibilityService extends
android.accessibilityservice.AccessibilityService {
private static final String TAG =
AccessibilityService.class.getSimpleName();
#Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
Log.d("ACCEVENT", accessibilityEvent.toString());
}
#Override
public void onInterrupt() {
Log.d("ACCEVENT", "onAccessibilityEvent Inturupt");
}
#Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.d(TAG, "onServiceConnected");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
FingerprintGestureController gestureController = getFingerprintGestureController();
Toast.makeText(getApplicationContext(), "Is available: " + gestureController.isGestureDetectionAvailable(), Toast.LENGTH_LONG).show();
Log.e(TAG, "Is available: " + gestureController.isGestureDetectionAvailable() );
FingerprintGestureController.FingerprintGestureCallback callback = new
FingerprintGestureController.FingerprintGestureCallback() {
#Override
public void onGestureDetectionAvailabilityChanged(boolean available) {
super.onGestureDetectionAvailabilityChanged(available);
Toast.makeText(getApplicationContext(), "Gesture available change to: " + available, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onGestureDetectionAvailabilityChanged " + available);
}
#Override
public void onGestureDetected(int gesture) {
super.onGestureDetected(gesture);
Toast.makeText(getApplicationContext(), "Gesture: " + gesture, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onGestureDetected " + gesture);
}
};
gestureController.registerFingerprintGestureCallback(callback, null);
}
}
#Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind " );
return super.onUnbind(intent);
}
}
This is my XML code for the service
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault|flagRequestFingerprintGestures"
android:canRequestFingerprintGestures="true" />
How do you know that your device supports gestures? Does TalkBack work with them? Otherwise, it's possible that the hardware vendor didn't report that the hardware detects gestures.
I'm looking at creating my gesture lock screen, which will be shown once screen is on.
It can not achieved using screen on\off broadcast receiver as they won't work if registered from manifest. And you can not keep service running all time.
My Service looks like.
public class CustomAccessibilityService extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
// event.getSource(), findFocus(int), getWindows(), or getRootInActiveWindow().
Toast.makeText(this, "onAccessibilityEvent" + event.getSource(), Toast.LENGTH_SHORT).show();
}
#Override
public void onInterrupt() {
Toast.makeText(this, "onInterrupt", Toast.LENGTH_SHORT).show();
}
#Override
protected boolean onGesture(int gestureId) {
Toast.makeText(this, "onGesture" + gestureId, Toast.LENGTH_SHORT).show();
return super.onGesture(gestureId);
}
}
Manifest file
<service
android:name=".CustomAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/service_configuration" />
</service>
And Service_configuration.xml as below.
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewAccessibilityFocused|typeViewAccessibilityFocusCleared"
android:accessibilityFeedbackType="feedbackAllMask"
android:canRetrieveWindowContent="true"
android:notificationTimeout="100"
android:packageNames="com.xxxx.gesturelockscreen" />
I am stuck on it.
I know that we need to activate accessibility service (Gesture lock screen) and I am using below code to check if it is enabled.
if (!isAccessibilityEnabled()) {
Toast.makeText(this, "Not Enabled!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);
} else {
//Good to go!
}
public boolean isAccessibilityEnabled() {
int accessibilityEnabled = 0;
final String ACCESSIBILITY_SERVICE_NAME = "com.xxx.gesturelockscreen/com.xxx.gesturelockscreen.CustomAccessibilityService";
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(), android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.d(TAG, "ACCESSIBILITY: " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.d(TAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.d(TAG, "***ACCESSIBILIY IS ENABLED***: ");
String settingValue = Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
Log.d(TAG, "Setting: " + settingValue);
if (settingValue != null) {
TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
Log.d(TAG, "Setting: " + accessabilityService);
if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVICE_NAME)) {
Log.d(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
Log.d(TAG, "***END***");
} else {
Log.d(TAG, "***ACCESSIBILIY IS DISABLED***");
}
return accessibilityFound;
}
Now I want when screen is on my custom accessibility service's need to be called.
Please help, any help is appreciated, thanks in advance.
I am trying to develop a setup of 2 applications (service app + client app) using AIDL. I have currently a setup of 3 modules:
android-agent-framework (android library module holding only the AIDL file)
android-agent (the service)
android-example-client (the client)
android-agent and android-agent-framework have a dependency to the first one to get access to the interface.
Whenever the client calls bindService() it gets false as return and in the ServiceConnection the onServiceConnected() is not called. Also in the service implementation the onBind() is not called. There is no error in the logs.
Here is the code:
android-agent activity:
public class MyCompanyStartActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(MyCompanyStartActivity.class.toString(), "Create MyCompanyStartActivity");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ComponentName service = startService(new Intent(this, MyCompanyRequestService.class));
Log.i("tag", service.getClassName() + "::" + service.getPackageName());
}
}
android-agent service:
public class MyCompanyRequestService extends Service {
#Override
public IBinder onBind(Intent intent) {
Log.i(MyCompanyRequestService.class.toString(), "Starting SmartRest Service");
return mBinder;
}
private final IMyCompanyRequestService.Stub mBinder = new IMyCompanyRequestService.Stub() {
#Override
public void sendData(String xid, String authentication, String data) throws RemoteException{
Log.i(MyCompanyRequestService.class.toString(), "sending data: " + data);
}
};
}
android-agent manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.android.agent" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyCompanyStartActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Services -->
<service
android:name="com.mycompany.android.agent.framework.MyCompanyRequestService"
android:process=":remote"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="MyCompanyRequestService"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
<!-- Permissions -->
</application>
</manifest>
android-example-client activity:
public class ClientStarter extends Activity {
protected IMyCompanyRequestService mycompanyRequestService = null;
#Override
public void onCreate(Bundle savedInstanceState) {
Log.i("tag","create client");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
if (mycompanyRequestService == null) {
printServices();
Intent it = new Intent("MyCompanyRequestService");
it.setPackage("com.mycompany.android.agent.framework");
Log.i("tag","before binding service: " + it.getAction() + "::" + it.getPackage());
boolean serviceBinding = getApplicationContext().bindService(it, connection, Context.BIND_AUTO_CREATE);
Log.i("tag", "service is bound: " + serviceBinding);
}
Handler handler = new Handler();
handler.postDelayed(new Runner(), 10000);
}
#Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
private ServiceConnection connection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("service", "Service connected");
mycompanyRequestService = IMyCompanyRequestService.Stub.asInterface(service);
Toast.makeText(getApplicationContext(), "Service Connected", Toast.LENGTH_SHORT).show();
Log.i("service", "Service connected");
}
#Override
public void onServiceDisconnected(ComponentName name) {
Log.i("service", "Service disconnected");
mycompanyRequestService = null;
Toast.makeText(getApplicationContext(), "Service Disconnected", Toast.LENGTH_SHORT).show();
Log.i("service", "Service disconnected");
}
};
private void printServices() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
Log.d("service", service.service.getClassName());
}
}
private class Runner implements Runnable {
#Override
public void run() {
Log.i("tag","starting");
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location loc;
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Log.e(ClientStarter.class.toString(), "Error", e);
} while(true) {
try {
if (mycompanyRequestService != null) {
loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.i(ClientStarter.class.toString(), loc.getLatitude() + " - " + loc.getLongitude() + " - " + loc.getAltitude());
mycompanyRequestService.sendData("test", "auth", String.valueOf(loc.getLatitude()) + "," + String.valueOf(loc.getLongitude()) + "," + String.valueOf(loc.getAltitude()));
} else {
Log.i(ClientStarter.class.toString(), "service not yet available");
}
Thread.sleep(5000);
} catch (InterruptedException e) {
Log.e(ClientStarter.class.toString(), "Error", e);
} catch (RemoteException e) {
Log.e(ClientStarter.class.toString(), "Error", e);
}
}
}
}
}
The printServices() call before trying to bind the service actually lists the service so it is running.
The log does not contain any errors and the client is in the end running in the loop but the service is still null.
Maybe someone encountered a similar issue before.
After going another round through all files I found my mistake.
I needed to change:
Intent it = new Intent("MyCompanyRequestService");
it.setPackage("com.mycompany.android.agent.framework");
to:
Intent it = new Intent("MyCompanyRequestService");
it.setPackage("com.mycompany.android.agent");
The package of the Intent needs to match the package of the app and not the package of the service.
Another reason why you could face this issue (at least I did) is that – from API level 30 – you are also required to declare the apps that you communicate to in the manifest, for example:
<queries>
<package android:name="com.your.app" />
</queries>
I am building an application with the GDK sneak peek and am having trouble getting speech recognition working in an immersive app. This is my first android project.
I tried to follow this: How can I use speech recognition without the annoying dialog in android phones
After making initial progress, I hit a problem where the RecognitionListener class is throwing Error 9, insufficient permissions.
I am using the GDK, which is Android-15.
Initialization of the Recognizer is in my onCreate() method:
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(new listener());
When I receiver a tap callback, I start listening:
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetector = new GestureDetector(context);
//Create a base listener for generic gestures
gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
#Override
public boolean onGesture(Gesture gesture) {
// Log.info(gesture.name());
if (gesture == Gesture.TAP) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
sr.startListening(intent);
return true;
}
return false;
}
});
return gestureDetector;
}
And here is the definition of my listener class:
class listener implements RecognitionListener
{
public void onReadyForSpeech(Bundle params)
{
Log.d(TAG, "onReadyForSpeech");
}
public void onBeginningOfSpeech()
{
Log.d(TAG, "onBeginningOfSpeech");
}
public void onRmsChanged(float rmsdB)
{
Log.d(TAG, "onRmsChanged");
}
public void onBufferReceived(byte[] buffer)
{
Log.d(TAG, "onBufferReceived");
}
public void onEndOfSpeech()
{
Log.d(TAG, "onEndofSpeech");
}
public void onError(int error)
{
Log.d(TAG, "error " + error);
// mText.setText("error " + error);
}
public void onResults(Bundle results)
{
String str = new String();
Log.d(TAG, "onResults " + results);
ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for (int i = 0; i < data.size(); i++)
{
Log.d(TAG, "result " + data.get(i));
str += data.get(i);
}
// mText.setText("results: "+String.valueOf(data.size()));
}
public void onPartialResults(Bundle partialResults)
{
Log.d(TAG, "onPartialResults");
}
public void onEvent(int eventType, Bundle params)
{
Log.d(TAG, "onEvent " + eventType);
}
}
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.medicalglass"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.medicalglass.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Immediately after the touch event comes in and I call start listening, the listener's onError method is called with error code 9, which denotes insufficient permissions. If anyone has any experience with android speech commands, or glass speech commands and know why this continues to fail I would be very appreciative. Thanks.
You have to request for permission on Android M onward.
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(activity, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED)
return;
else {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)) {
Toast.makeText(activity, "Record audio is required", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.RECORD_AUDIO}, RECORD_AUDIO);
}
}
Start by changing this code:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
To this code:
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getApplication().getPackageName());
speechRecognizer.startListening(intent);
EDIT:
Add this to your manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
If you have an error please past your LogCat.
This should be working now with API Level 19 and the two permissions mentioned above.
Voice recognition not available offline (yet?), see this Google Glass requested feature for allowing offline voice recognition (Issue 305)
In my situation (SpeechRecognizer permissions that used to be sufficient in API 8, no longer sufficient in API 29), I solved the problem by going to Settings > Permissions > Permissions > Microphone > Slide (from OFF to ON) for my app:
I have written a simple application to fetch the network details where i have used an AlarmManager to continuously run a piece of code in the background(IntentServiceImpl class)..
I am invoking the AlarmManager class from the BroadcastReceiver, and i have registered the BroadcastReceiver(BroadcastReceiverImpl class) for BOOT_COMPLETE event... it is working properly in the android emulator but it is not working in Android Device(i have checked it in Samsung Galaxy s2 and Samsung Galaxy Tab) Here is the source code..
BroadcastReceiverImpl.java
public class BroadcastReceiverImpl extends BroadcastReceiver {
private static final String LOG_TAG = BroadcastReceiverImpl.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG_TAG, "Inside the BroadcastReceiver Class");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND,QOSConstants.TIME_DELAY_IN_SECONDS);
Intent serviceIntent = new Intent(context, IntentServiceImpl.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getService(context, QOSConstants.REQUEST_CODE,serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
//After after 60 seconds
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
QOSConstants.MILLISECONDS * QOSConstants.TIME_DELAY_IN_SECONDS, pendingIntent);
Log.d(LOG_TAG, "Exiting the QOSBroadcastReceiver Class");
}
}
IntentServiceImpl.java
public class IntentServiceImpl extends IntentService implements LocationListener {
private String strNetworkInfo;
private LocationManager locationManager;
private QOSDatabaseHelper qosDatabaseHelper;
private NetworkDetailsVO networkDetailsVO ;
private static final String LOG_TAG = QOSIntentService.class.getName();
private QOSServlet qosServlet;
private QOSPhoneStateListner qosPhoneStateListner;
public QOSIntentService() {
super(LOG_TAG);
Log.d(LOG_TAG , ": Inside NetworkInfoService() constructor");
}
#Override
public void onCreate() {
super.onCreate();
qosServlet = new QOSServlet();
networkDetailsVO = new NetworkDetailsVO();
qosPhoneStateListner = new QOSPhoneStateListner();
qosDatabaseHelper = new QOSDatabaseHelper(getApplicationContext());
}
#Override
protected void onHandleIntent(Intent intent) {
Log.i("LOG_TAG: ", "Inside onHandleIntent(Intent) method Fetching Network Details ");
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
final TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null && networkInfo.isConnected()){
String networkState = getNetworkStateString(networkInfo.getState());
String stateString = networkInfo.toString().replace(',', '\n');
strNetworkInfo = String.format(QOSConstants.NETWORK_STATE_DISPLAY_FORMAT,
networkInfo.getTypeName(),networkState,stateString);
networkDetailsVO.setNetworkState(networkState);
networkDetailsVO.setNetworkType(networkInfo.getTypeName());
networkDetailsVO.setRoaming(String.valueOf(networkInfo.isRoaming()));
networkDetailsVO.setReason(networkInfo.getReason());
networkDetailsVO.setFailOver(String.valueOf(networkInfo.isFailover()));
networkDetailsVO.setNetworkAvailable(String.valueOf(networkInfo.isAvailable()));
networkDetailsVO.setNetworkConnectivity(String.valueOf(networkInfo.isConnected()));
if(networkInfo.getExtraInfo()!=null)
networkDetailsVO.setExtraNetworkInfo(networkInfo.getExtraInfo());
else
networkDetailsVO.setExtraNetworkInfo(QOSConstants.NETWORK_EXTRA_INFO_NOT_AVAILABLE);
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, QOSConstants.MINIMUM_TIME, QOSConstants.MINIMUM_DISTANCE, this);
Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));
if(location!=null){
onLocationChanged(location);
QOSGeocoder qosGeocoder = new QOSGeocoder();
networkDetailsVO.setLocationAddress(qosGeocoder.getLocationAddress(getApplicationContext(), location));
}else{
networkDetailsVO.setLatitude(0.0);
networkDetailsVO.setLongitude(0.0);
}
// Identify the connectivity type. WI-FI/MOBILE.
if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
networkDetailsVO.setSignalStrength(wifiManager.getConnectionInfo().getLinkSpeed());
}else if(networkInfo.getType() == ConnectivityManager.TYPE_MOBILE){
telephonyManager.listen(qosPhoneStateListner, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
networkDetailsVO.setSignalStrength(qosPhoneStateListner.getStrSignalStrength());
}
networkDetailsVO.setNetworkInfo(strNetworkInfo);
networkDetailsVO.setSimSerialNumber(telephonyManager.getSimSerialNumber());
networkDetailsVO.setOperatorName(telephonyManager.getNetworkOperatorName());
networkDetailsVO.setDateTime(new Date().toString());
Log.i(LOG_TAG , "Network Details :- " + "\t" + networkInfo.toString());
//qosDatabaseHelper = new QOSDatabaseHelper(getApplicationContext());
qosDatabaseHelper.saveRecord(networkDetailsVO);
qosServlet.invokeServlet(networkDetailsVO);
Log.i(LOG_TAG , " Network details are saved to the Database,exiting onHandleIntent(Intent) method ");
}
#Override
public void onLocationChanged(Location location) {
Log.i(LOG_TAG, " : Inside onLocationChanged(Location) method ");
networkDetailsVO.setLatitude(location.getLatitude());
networkDetailsVO.setLongitude(location.getLongitude());
Log.i(LOG_TAG, " : Exiting onLocationChanged(Location) method ");
}
#Override
public void onProviderEnabled(String provider) {
Log.i(LOG_TAG, ": Provider is Enabled !! ");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, QOSConstants.MINIMUM_TIME,
QOSConstants.SECOND_MINIMUM_DISTANCE, this);
}
#Override
public void onProviderDisabled(String string) {
Log.i(LOG_TAG, ": Provider is Disabled !! ");
}
#Override
public void onStatusChanged(String strStatus, int arg1, Bundle bundle) {
Log.i(LOG_TAG, " : Inside onStatusChanged(String,int,Bundle) method ");
}
private String getNetworkStateString(NetworkInfo.State state){
Log.i(LOG_TAG, ": Inside getNetworkStateString(NetworkInfo.State)");
String stateString = QOSConstants.NETWORK_STATE_UNKNOWN;
switch(state)
{
case CONNECTED:
stateString = QOSConstants.NETWORK_STATE_CONNECTED;
break;
case CONNECTING:
stateString = QOSConstants.NETWORK_STATE_CONNECTING;
break;
case DISCONNECTED:
stateString = QOSConstants.NETWORK_STATE_DISCONNECTED;
break;
case DISCONNECTING:
stateString = QOSConstants.NETWORK_STATE_DISCONNECTING;
break;
case SUSPENDED:
stateString = QOSConstants.NETWORK_STATE_SUSPENDED;
break;
default:
stateString = QOSConstants.NETWORK_STATE_UNKNOWN;
break;
}
return stateString;
}
#Override
public void onDestroy() {
super.onDestroy();
if(qosDatabaseHelper!=null){
qosDatabaseHelper.closeDatabase();
}
}
}
#Override
public void onProviderDisabled(String string) {
Log.i(LOG_TAG, ": Provider is Disabled !! ");
}
/*#Override
public IBinder onBind(Intent intent) {
Log.i("LOG_TAG: ", "Inside onBind(Intent) method");
return null;
}
*/
#Override
public void onStatusChanged(String strStatus, int arg1, Bundle bundle) {
Log.i(LOG_TAG, " : Inside onStatusChanged(String,int,Bundle) method ");
}
private class SignalStrengthDetector extends PhoneStateListener{
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
networkDetailsVO.setSignalStrength(signalStrength.getGsmSignalStrength());
Log.i(LOG_TAG, "Signal Strength :-" + String.valueOf(signalStrength.getGsmSignalStrength()));
}
}
private String getNetworkStateString(NetworkInfo.State state){
Log.i(LOG_TAG, ": Inside getNetworkStateString(NetworkInfo.State)");
String stateString = NetworkInfoConstants.NETWORK_STATE_UNKNOWN;
switch(state)
{
case CONNECTED:
stateString = NetworkInfoConstants.NETWORK_STATE_CONNECTED;
break;
case CONNECTING:
stateString = NetworkInfoConstants.NETWORK_STATE_CONNECTING;
break;
case DISCONNECTED:
stateString = NetworkInfoConstants.NETWORK_STATE_DISCONNECTED;
break;
case DISCONNECTING:
stateString = NetworkInfoConstants.NETWORK_STATE_DISCONNECTING;
break;
case SUSPENDED:
stateString = NetworkInfoConstants.NETWORK_STATE_SUSPENDED;
break;
default:
stateString = NetworkInfoConstants.NETWORK_STATE_UNKNOWN;
break;
}
return stateString;
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pervazive.qualityofservice_v01"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.pervazive.qualityofservice_v01.activity.QOSActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.pervazive.qualityofservice_v01.intentservice.<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pervazive.qualityofservice_v01"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.pervazive.qualityofservice_v01.activity.QOSActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.pervazive.qualityofservice_v01.intentservice.IntentServiceImpl"
android:enabled="true" >
</service>
<receiver android:name="com.pervazive.qualityofservice_v01.broadcastreceiver.BroadcastReceiverImpl" >
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Please let know what is the error...please let me know where can i find the log file in Android device... (because it is running on the Android Emulator but not running i=on the Android device...) Thanks in Advance
Right click on your project> Android Tools> Export as signed android application.
Fill required fields and you are good to go.