How to Latitude and Longitude in android - android

I have developed simple application to display the latitude and longitude in android... but it is showing the null pointer exception... please let me know what is the error..
My Activity class
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class SignalStrengthActivity extends Activity implements LocationListener{
private LocationManager locationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
#Override
protected void onResume() {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
super.onResume();
}
#Override
protected void onPause() {
locationManager.removeUpdates(this);
super.onPause();
}
#Override
public void onLocationChanged(Location location) {
if (location != null) {
Toast.makeText(this, "Latitude and Longitude " + location.getLatitude() + " " +
location.getLongitude(), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Latitude and Longitude Offline ", Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
My Manifest file :-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.latitudelongitude"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.latitudelongitude.SignalStrengthActivity"
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>
</application>
</manifest>
when i execute the application it does not display anything.. indicating that the location object is null... please let me know how to fix the problem

Go through the link below:
Link 1

Related

Can't use heart_rate sensor in android studio

I have a problem getting data from my heart_rate_sensor. If I use a light sensor, it works perfectly but when it comes to the other sensor it doesn´t.
Here`s my activity.java file:
package com.example.smartwatchprueba2;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import com.example.smartwatchprueba2.databinding.ActivityMainBinding;
public class MainActivity extends Activity {
private TextView mTextView,mTextView1;
private ActivityMainBinding binding;
private SensorManager sensorManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
sensorManager = (SensorManager) getSystemService(this.SENSOR_SERVICE);
Sensor sensorLuz = sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
Sensor sensorRitmoCardiaco = sensorManager.getDefaultSensor(Sensor.TYPE_HEART_RATE);
sensorManager.registerListener(listener, sensorLuz, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(listener1, sensorRitmoCardiaco, SensorManager.SENSOR_DELAY_NORMAL);
mTextView = binding.textView;
mTextView1=binding.textView1;
}
#Override
protected void onDestroy() {
super.onDestroy();
if (sensorManager != null) {
sensorManager.unregisterListener(listener);
sensorManager.unregisterListener(listener1);
}
}
SensorEventListener listener=new SensorEventListener() {
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
float value = sensorEvent.values[0];
mTextView.setText("Current light level is " + value + " lx");
}
#Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
SensorEventListener listener1=new SensorEventListener() {
#Override
public void onSensorChanged(SensorEvent sensorEvent) {
float value1 = sensorEvent.values[0];
mTextView.setText("Current heart rate is: " + value1);
}
#Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
I thought it could be something with the permission, but I have included Body_sensors permission and it doesnt work. Heres my manifest file so you can see all the permissions.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smartwatchprueba2">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BODY_SENSORS" />
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#android:style/Theme.DeviceDefault">
<uses-library
android:name="com.google.android.wearable"
android:required="true" />
<!--
Set to true if your app is Standalone, that is, it does not require the handheld
app to run.
-->
<meta-data
android:name="com.google.android.wearable.standalone"
android:value="true" />
<activity
android:name=".MainActivity"
android:exported="true"
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>

Sending data to fragment from service

i have a string in my service, and i want to send it to fragment. But it seems onReceive() method on my BroadcastReceiver never called, and i just can't figured out why. Thanks for answers.
My Service Class
public class Servis extends Service {
static final String MAP = "MyService";
static final String USERID = "";
public static final String FILEPATH = "filepath";
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
System.out.println("starting service");
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
String id = intent.getStringExtra("UserID");
System.out.println(id);
Intent intent1 = new Intent(MAP);
intent1.putExtra(USERID, id);
sendBroadcast(intent1);
return START_REDELIVER_INTENT;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(MAP, "onCreate");
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(MAP, "onDestroy");
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(MAP, "onStart");
}
}
My Fragment Class
package com.example.mobilproje;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapFragment extends FragmentActivity{
private GoogleMap googleHarita;
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
handleResult(bundle);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment);
registerReceiver(receiver, new IntentFilter(
Servis.MAP));
if (googleHarita == null) {
googleHarita = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.haritafragment))
.getMap();
if (googleHarita != null)
LatLng EvKoordinat = new LatLng(40.7994211,29.9517352);
googleHarita.addMarker(new MarkerOptions().position(EvKoordinat).title("Ev"));
googleHarita.moveCamera(CameraUpdateFactory.newLatLngZoom(EvKoordinat, 13));
}
}
}
#Override
public void onResume() {
registerReceiver(receiver, new IntentFilter(
Servis.MAP));
}
private void handleResult(Bundle bundle) {
if (bundle != null) {
String string = bundle.getString(Servis.USERID);
System.out.println("string");
}
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobilproje"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/nfc"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:theme="#style/mytheme"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<action android:name="android.nfc.action.TAG_DISCOVERED" />
</intent-filter>
<meta-data
android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="#xml/nfc_tech_filter" />
</activity>
<activity
android:name=".MapFragment"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCgPPczPpQA_00_v-nTux_WuGuaO9egUpc" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<service android:enabled="true" android:name=".Servis" android:process=":localservice"/>
</application>
<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"/>
</manifest>

my application stops working as soon as launched

(ANDROID GPS)
i made this application to send gps coordinates via sms but the app crashes(as soon as i open it) can somebody help me
logcat images
logcal image1
log cat image 2
MainActivity.java
package com.adzz.gps;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
import android.telephony.SmsManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
String m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager manager= (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener =new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
final String phoneNumber="9453603045";
double lat1=location.getLatitude();
Double d1= new Double(lat1);
double longi1=location.getLongitude();
Double d2=new Double(longi1);
m="latitude = "+ d1.toString() + "and latitude = "+ d2.toString();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, m, null, null);
TextView lat= (TextView) findViewById(R.id.lat);
TextView longi=(TextView) findViewById(R.id.longi);
lat.setText("latitude = "+lat1);
longi.setText("longitude ="+longi1);
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 25,
listener);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast toast=Toast.makeText(Main.this, m, 5000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/longi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<Button
android:id="#+id/button1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adzz.gps001"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.adzz.gps001.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>
In your manifest file you must include
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
Read the documentation under "Requesting User Permissions" for more details.
http://developer.android.com/guide/topics/location/strategies.html
Your app is failing because you need to add the android.permisssion.ACCESS_FINE_LOCATION permission to your manifest file.

application doesn't seem to do anything

(ANDROID GPS)
i made this application to send gps coordinates via sms but it doesn't seem to do any thing can anybody tell me what am i missing??
this is what logcat have to say on pressing the button
logcat image1
logcat image2
MainActivity.java
package com.adzz.gps;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.content.Context;
import android.telephony.SmsManager;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
String m;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager manager= (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener =new LocationListener() {
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
final String phoneNumber="9453603045";
double lat1=location.getLatitude();
Double d1= new Double(lat1);
double longi1=location.getLongitude();
Double d2=new Double(longi1);
m="latitude = "+ d1.toString() + "and latitude = "+ d2.toString();
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, m, null, null);
TextView lat= (TextView) findViewById(R.id.lat);
TextView longi=(TextView) findViewById(R.id.longi);
lat.setText("latitude = "+lat1);
longi.setText("longitude ="+longi1);
}
};
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 25,
listener);
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast toast=Toast.makeText(Main.this, m, 5000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/lat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/longi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
<Button
android:id="#+id/button1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adzz.gps001"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses- permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.adzz.gps001.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>
How about adding the permission for SMS in your AndroidManifest.xml?
<uses-permission android:name="android.permission.SEND_SMS" />

Issue with trying to access Android Location Data

So I am trying to a simple Lat Long for my phone.
I am testing using my own phone which is on FirmWare Level 2.3.6.
This is the main code:
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
public class MainActivity extends FragmentActivity {
private LocationManager locationManager = null;
private LocationListener locationListener = null;
// private LocationListener onLocationChange=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, locationListener);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
System.out.println("I got here");
}
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
/*
* System.out.println("This happened"); String message =
* "Longitude:" + loc.getLongitude() + " Latitude: " +
* loc.getLongitude(); Toast.makeText( getBaseContext(),
* "Location changed: Lat: " + loc.getLatitude() + " Lng: " +
* loc.getLongitude(), Toast.LENGTH_SHORT).show();
*/
System.out.println("IN ON LO
CATION CHANGE, lat="
+ loc.getLatitude() + ", lon=" + loc.getLongitude());
}
#Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
}
This is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cloud.pushpin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.cloud.pushpin.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>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyArmkmqy3i-wNsVH0f8RH15OsaR4lv4Sp8" />
</application>
<permission
android:name="com.cloud.pushpin.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.cloud.pushpin.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
</manifest>
So basically what I Am seeing is that nothing happens when I start up the app. When I shake the phone, I get the message "I got here" a bunch of times, but never the latitude and longitude displaying. I don't understand why the callback is not occurring. The other permissions are for later when I implement the google maps api. GPS is enabled on the phone as well as mobile data.

Categories

Resources