I try to use the DDMS location mockup with a very simple Log.i() call in the functions of all LocationListener attributes. When I start the program I get the Log.i() message for onStatusChanged() in my LogCat, so I think that I registered the listener correctly. But when I put a location into the VM using the DDMS menus in Eclipse I get nothing. No change in LogCat, Console, or inside the VM. As if nothing happened at all.
How can I even start to find the problem?
Here is the code for my simple GPS Event logger:
public class gpsActivity extends Activity {
private static final String TAG="gpsActivity";
private static TextView label;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
label = (TextView) findViewById(R.id.label);
LocationManager manager = (LocationManager) this
.getSystemService(LOCATION_SERVICE);
LocationListener listener = new LocationListener() {
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
Log.i(TAG, "onStatusChanged: "+arg0);
}
#Override
public void onProviderEnabled(String arg0) {
Log.i(TAG, "onProviderEnabled: "+arg0);
}
#Override
public void onProviderDisabled(String arg0) {
Log.i(TAG, "onProviderDisabled: "+arg0);
}
#Override
public void onLocationChanged(Location location) {
Log.i(TAG, "onLocationChanged");
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2, 0,
listener);
setContentView(R.layout.main);
}
}
and here the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.example.gps"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".gpsActivity"
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>
Also as another side note, I am using a German Windows XP and found out that there are problems with GPS packages, because in a German system the number format for 1/2 is not 0.5 but 0,5 which leads to errors in the GPS parser grammar. But also changing my systems number format to English(US) didn't change that I don't get any message, exception or result at all.
Be sure that your device/emmulator is selected in the device menu of DDMS.
EDIT:
Oops. I should have seen these earlier:
You are using NETWORK_PROVIDER. In order to receive mock locations from DDMS (and I think also the command line through geo), you must register the GPS_PROVIDER. Your code works by changing the last line of the onCreate method:
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2, 0,
listener);
NETWORK_PROVIDER is signals through the phone network (e.g 3G) or WIFI
There where 3 problems involved. Because finding all of them took more then 12 hours of pure research time please excuse that I won't even try to find the according sources again.
The problems involved where:
You should use Google API <version> as target and not Android <version> because there are nesssesary things not activated in the latter.
You should send the updates via telnet and not DDMS because DDMS creates some uncertain, undocumented and not logged results, especially if you are using a non-US version of Windows.
The time zone in your emulator is automatically chosen, but maybe to the wrong locale (as mine is Germany, for example). You can switch it off inside a running emulator in the Android settings and also choose your correct locale. This only seems to appear in older versions of Android.
Good luck to everybody who tries his way to this kind of errors!
Related
I want to implement a functionality where on the click of a button the menu of android appear which we see on the long tap of power button, and then the user can choose to turn the device off.
I'm talking about this menu
I need it to be without root. There's an app on store which does it without requiring root.
https://play.google.com/store/apps/details?id=com.jjo.lockScreenButton
I might be late but this is possible. You have to use an accessibility service for that, which the user must allow access to. Also, keep in mind that accessibility services are meant to help handicapped people and not for this use case.
1. Create a PowerMenuService
public class PowerMenuService extends AccessibilityService {
private BroadcastReceiver powerMenuReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if(!performGlobalAction(intent.getIntExtra("action", -1)))
Toast.makeText(context, "Not supported", Toast.LENGTH_SHORT).show();
}
};
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {}
#Override
public void onInterrupt() {}
#Override
public void onCreate() {
super.onCreate();
LocalBroadcastManager.getInstance(this).registerReceiver(powerMenuReceiver, new IntentFilter("com.yourapp.ACCESSIBILITY_ACTION"));
}
#Override
public void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(powerMenuReceiver);
}
}
make sure to replace com.yourapp with your app package
2. Register service in Manifest
Add the following under the <application> tag:
<service
android:name=".PowerMenuService"
android:enabled="true"
android:exported="true"
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/accessibility_service" />
</service>
3. accessibility_service.xml
In your xml resource directory create a file named accessibility_service.xml which contains the following:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:packageNames="com.yourapp" />
Again, replace com.yourapp. You should also provide a description. More info here: https://developer.android.com/guide/topics/ui/accessibility/services.html#service-config
4. Show menu
ComponentName component = new ComponentName(getApplicationContext(), PowerMenuService.class);
getApplicationContext().getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
Intent intent = new Intent("com.yourapp.ACCESSIBILITY_ACTION");
intent.putExtra("action", AccessibilityService.GLOBAL_ACTION_POWER_DIALOG);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
Taken from: https://github.com/farmerbb/Taskbar
You cannot do this.
The dialog itself is a part of the system, and is not exposed to application developers in any way.
You could mimic the dialog yourself, but the framework also does not expose a method for you to programmatically initiate a shutdown or reboot.
If you really want to go down this route, you could also require root access for your application and use these options for shutting down or rebooting the device.
If your application is a standard consumer application, I recommend letting them shutdown their device using their devices' standard interfaces for doing so.
I am using Google API V2 in an Android application.
I want to get my current location and add a marker on it.
I am using my friend device. when I run the code, it shows me my-friend's house location. I am really shocked why.
could anyone please help me getting my current location..
Thats because you're first loading the LastKnownLocation, because it's your friends phone it will show your friend last known location (his house).
The code looks fine, I think you have to wait untill it finds the real location.
(make sure you got all permissions, and gps is on)
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
When you call locationManager.getLastKnownLocation(provider) your are retrieving the last location of the GPS that can be like you say when your friend was at home.
If you want to have the location where you are at, you should call requestSingleUpdate for just one time update or requestLocationUpdates for a continuous update.
An example for the one time update with a button listener:
btnupdate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
locationManager.requestSingleUpdate(criteria, new LocationListener() {
#Override
public void onLocationChanged(Location location) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}, getMainLooper());
}
});
This implements a listener for the LocationManager, that gives you the posibility to do some actions depending of the Location status.
In your case, you have implemented the listener on the activity, so just try calling:
locationManager.requestSingleUpdate(criteria, this);
This request just single update to show your present location
I am coming with an answer to my question. I don't know if it will work with all people facing the same problem. I just tried to restart the mobile device. After it restarts, it gets the correct current location. So it was a hardware problem though I thought it is something in the code..
Can someone please help me out with this.
I want to get notified when my phone battery becomes low.
My androidManifest file looks like this:
<receiver android:name="com.dac.BatteryChangedBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_LOW" />
</intent-filter></receiver>
and my receiver file is:
public class BatteryChangedBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if(Intent.ACTION_BATTERY_LOW.equalsIgnoreCase(intentAction))
Toast.makeText(context, "Battery Power Low or Okay", Toast.LENGTH_LONG).show();
}
}
I am changing the battery level using the telnet command. My phone battery does change but I do not get any toast message. I have even tried registering the receiver using code
You probably need to add the following permission to your manifest file.
<uses-permission android:name="android.permission.BATTERY_STATS" />
The "com. in the android:name looks questionable. If BatteryChangedBroadcastReceiver is in the default package for the application the name should begin with the dot. If not, it should be fully qualified. If you are writing your code in package com -- well -- don't do that, it hurts.
I'm currently testing gps locations in Android(with SDK Tools rev 21.0.1. I tried the standard procedure:
Create an emulator with GPS enabled(tried multiple version, from 2.1 to 4.2)
Start the emulator
telnet to localhost:5554
Type geo fix long lat
In theory, this should set the gps to .
But the following signs indicate the location is not properly set:
1. When I go to maps.google.com or open Google map, it always complains that cannot decide my location and they always ask me to switch on wifi.
2. The GPS never seems to be activated -- no icons on the status bar.
Also, I tried DDMS(which is deprecated and of course I tried Monitor as well). Nothing seems to happen.
I went previous links pointed here:
Location not being updated on new geo fix in android emulator
Android - Unable to get the gps location on the emulator
GPS on emulator doesn't get the geo fix - Android
But all those links do not seem to help. There are some bug report on android project page:
http://code.google.com/p/android/issues/detail?id=13046
but it was a pretty old issue and a final solution wasn't reached.
I'm wondering if anyone else has experienced similar issue and please offer some help. Thanks.
hope You will see icon and it will work.working on mine. APi Level 10
package com.example.locationsddf;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
LocationManager lm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.tv);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onLocationChanged(Location arg0) {
tv.setText("Latitude: "+arg0.getLatitude()+" \nLongitude: "+arg0.getLongitude());
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationsddf"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.locationsddf.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>
When icon is notified then you can just simply get the location by lm.getLastKnowLocationit will work
Sometimes there are issues with AVD itself. Try switching to another CPU architecture. I had similar issues and that was resolved by switching between ARM and Intel. Sometimes AVD with Google services enabled also helped
Also play around with "Allow Mock Locations" in Developers options
As an alternative, you could try this client:
https://github.com/RomanTheLegend/Pokemon_NoGo/tree/master/Sources/PokemonNoGo
I wrote it to play Pokemons, but it works with any GPS-based app. All it does is accept coordinates from desktop client and mocks main system GPS provider - LocationManager.GPS_PROVIDER
Last but not least: in AVD you can replay .kml files. There are many services which could help you generate one. Or just install "Lockito" and generate fake coordinates from Android
So i've just started to use the new Sony Xperia Tablet S Small App SDK. I'm no realy noob, developed many personal apps before but this has got me stumped.
I've loaded up the Sample project in Eclipse (all correctly configured), sorted some of the errors out, compiled to my device and when I launch the Small App from the launcher at the bottom of the device, it force closes - it's the same with every sample/app that I tried making with the SDK.
I'm attached below my MainApplication.java and AndroidManifest.xml in the hope that someone may be able to see where the issue lies. Don't understand as it's created as per the book. Any help really is appreciated please.
MainApplication.java:
package com.sony.thirdtest;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.view.View;
import android.widget.Toast;
import com.small.sonyapptest.R;
import com.sony.smallapp.SmallAppWindow;
import com.sony.smallapp.SmallApplication;
public class MainApplication extends SmallApplication {
private Configuration mConfig;
#Override
public void onCreate() {
super.onCreate();
mConfig = new Configuration(getResources().getConfiguration());
setContentView(R.layout.activity_main);
setTitle(R.string.app_name);
SmallAppWindow.Attributes attr = getWindow().getAttributes();
attr.minWidth = 200;
attr.minHeight = 200;
attr.width = 400;
attr.height = 300;
attr.flags |= SmallAppWindow.Attributes.FLAG_RESIZABLE;
getWindow().setAttributes(attr);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainApplication.this, R.string.hello, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onStart() {
super.onStart();
}
#Override
public void onStop() {
super.onStop();
}
#Override
protected boolean onSmallAppConfigurationChanged(Configuration newConfig) {
int diff = newConfig.diff(mConfig);
mConfig = new Configuration(getResources().getConfiguration());
// Avoid application from restarting when orientation changed
if ((diff & ActivityInfo.CONFIG_ORIENTATION) != 0) {
return true;
}
return super.onSmallAppConfigurationChanged(newConfig);
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.small.sonyapptest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="com.sony.smallapp.permission.SMALLAPP" />
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name">
<uses-library android:name="com.sony.smallapp.framework" />
<service
android:name="com.small.sonyapptest.MainApplication"
android:exported="true" >
<intent-filter>
<action android:name="com.sony.smallapp.intent.action.MAIN" />
<category
android:name="com.sony.smallapp.intent.category.LAUNCHER" />
</intent-filter>
</service>
</application>
</manifest>
It seems that there is nothing wrong with your code. So I think the problem is with how you build the project. More exactly, how the small apps framework is included: it shouldn't be!
If you simply add the jar (from the Sony SDK) via "Java build path" -> "Add external Jar", then the classes of the api jar will be included with the application. The problem is those are only stub classes, so you can get one of two possible exceptions.
A simple and quick way to get around this (and still using the standard android sdk, and not switch to the Sony SDK) is the following:
Create a java project, and call it "SmallAppApi" for example
Inside the java project add the small app jar via "Add external jar"
In the last tab in the "Java build path" screen, called "Order and Export" make sure the small app jar is exported.
In the android project, in the "Java build path" screen, in "Projects" tab simply add the java project SmallAppApi (and remove the small app jar).
With this setup the small app jar will be used only when building. This worked fine for me.
In the service tag change android:name="com.small.sonyapptest.MainApplication" to android:name="MainApplication"
Thanks for the responses, all working now after chaning the service tag to just MainApplication.