I'm working on an Android Project where i'm using AddProximityAlert method, as you already know this method let you set a proximity alert for the location given by the position (latitude, longitude) and the given radius, and notify you if you are so close to it.
so i was working on that for three days ago and i was getting the same probleme again and again..
in bref: this is my simple code.
#MainActivity.java
package com.example.proximityalert;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity implements LocationListener {
LocationManager lm;
//Defining Latitude & Longitude
double lat=37.422006 ,long1=-122.084095;
//Defining Radius
float radius=1000;
//Intent Action
String ACTION_FILTER = "com.example.proximityalert";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//i'm registering my Receiver First
registerReceiver(new ProximityReciever(), new IntentFilter(ACTION_FILTER));
//i'm calling ther service Location Manager
lm=(LocationManager) getSystemService(LOCATION_SERVICE);
//for debugging...
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
//Setting up My Broadcast Intent
Intent i= new Intent(ACTION_FILTER);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
//setting up proximituMethod
lm.addProximityAlert(lat, long1, radius, -1, pi);
}
#Override
//just For debugging to See the distance between my actual position and the aproximit point
public void onLocationChanged(Location newLocation) {
Location old = new Location("OLD");
old.setLatitude(lat);
old.setLongitude(long1);
double distance = newLocation.distanceTo(old);
Log.i("MyTag", "Distance: " + distance);
}
#Override
public void onProviderDisabled(String arg0) {}
#Override
public void onProviderEnabled(String arg0) {}
#Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}
#ProximityReceiver.java
package com.example.proximityalert;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
public class ProximityReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Key for determining whether user is leaving or entering
String key = LocationManager.KEY_PROXIMITY_ENTERING;
//Gives whether the user is entering or leaving in boolean form
boolean state = intent.getBooleanExtra(key, false);
if(state){
// Call the Notification Service or anything else that you would like to do here
Log.i("MyTag", "Welcome to my Area");
Toast.makeText(context, "Welcome to my Area", Toast.LENGTH_SHORT).show();
}else{
//Other custom Notification
Log.i("MyTag", "Thank you for visiting my Area,come back again !!");
Toast.makeText(context, "Thank you for visiting my Area,come back again !!", Toast.LENGTH_SHORT).show();
}
}
}
*#the probleme is when i run the program , the BroadcastReceiver(ProximityReciever) never never called by the system even if i'm veryyy close to the proximit point, and even if the debugger shows me that distance between the two locations is < 1000m :/
I just figured out somthing about this topic and why the addProximityAlert sames to be not working, I'm sharing this with you because I noticed that some people asked the same question before and they don't get any answer!
The answer was just in front of me but i didn't pay attention to it, so when i was reading the Android officiel documentation (here) i saw this sentence "Due to the approximate nature of position estimation, if the device passes through the given area briefly, it is possible that no Intent will be fired"
what is that mean? it means when you are testing your app on the AVD and u send a gps coordinates(latitude, longitude) from the DDMS to AVD its really hard to
simulate the real aspect of a gps, (because in the first place u pick some point to be your proximPoint and just after that you choose anthor point very far from the proximPoint to see if its work) and thats not what it's happing with a real device.
so the solution is to test your app on a real device or with the DDMS try to change the coordiantes very slowly until you are in the zone wanted.
Related
I'm trying to add an activity as a feature to an app I'm building where, the API will return a lat long, and with this lat long I will load google street view. Which with the movement of the device, will rotate the 360 degree angle of the position. I'm struggling on the movement part of the device. Using your fingers on the screen you can rotate. I wonder if anyone can point me in the right direction in getting the device movement to affect the position of the street view?
The code I have so far is:
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
import com.google.android.gms.maps.StreetViewPanorama;
import com.google.android.gms.maps.StreetViewPanoramaFragment;
import com.google.android.gms.maps.StreetViewPanoramaOptions;
import com.google.android.gms.maps.StreetViewPanoramaView;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.StreetViewPanoramaCamera;
import com.google.android.gms.maps.model.StreetViewPanoramaLocation;
public class MainActivity extends FragmentActivity
implements OnStreetViewPanoramaReadyCallback {
private static final String TAG = MainActivity.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StreetViewPanoramaFragment streetViewPanoramaFragment =
(StreetViewPanoramaFragment) getFragmentManager()
.findFragmentById(R.id.streetviewpanorama);
streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
}
#Override
public void onStreetViewPanoramaReady(final StreetViewPanorama panorama) {
final long duration = 1000;
float tilt = 30;
float bearing = 90;
final StreetViewPanoramaCamera camera = new StreetViewPanoramaCamera.Builder()
.zoom(panorama.getPanoramaCamera().zoom)
.bearing(bearing)
.tilt(tilt)
.build();
panorama.setPosition(new LatLng(52.208818, 0.090587));
panorama.setStreetNamesEnabled(false);
panorama.setZoomGesturesEnabled(false);
panorama.setOnStreetViewPanoramaChangeListener(new StreetViewPanorama.OnStreetViewPanoramaChangeListener() {
#Override
public void onStreetViewPanoramaChange(StreetViewPanoramaLocation streetViewPanoramaLocation) {
if (streetViewPanoramaLocation != null) {
panorama.animateTo(camera, duration);
}
Log.d(TAG, "TESTINGGGGGGGGGG");
}
});
}
}
I'm not sure given your question, so comment if i'm wrong but it seems you're able to rotate through this instruction
panorama.animateTo(camera, duration);
and you're moving to a specific location using the "camera" variable you built before.
So, if i understand correctly what you are trying to do, you have to check for mobilephone sensors (accelerometer & position) to get the motion then apply the correct motion to the panorama. Take a look at android sensor documentation in order to get the proper listeners (or how to register a sensor usage) then build the correct camera object according to the acceleration registered by the phone (left acceleration -> rotating left, right acceleration --> rotating right).
If you need a code example i'd suggest you to look this question which has some other links to help you using sensors and getting more doc.
If this does not help, comment and/or clarify the question.
I have created a distance calculator for my final year project. The calculator should display the users current location and then display a marker on the map when pressed. The distance will be displayed from the users current location to the marker.
I have retrieved the users location and stored as a variable which I use in my code but I get thrown with the java.lang.IllegalStateException: System services not available to Activities before onCreate() error. I've tried placing my code from the start in the onCreate() method but this doesn't work either. Any help would be greatly appreciated. I have been trying for hours to get it working but no luck. When I try to place the (LocationManager)getSystemService(Context.LOCATION_SERVICE); in the onCreate() it requires a permission and I've tried everything.
Here is my code
package com.example.matthewmcnabb.moyola;
import android.Manifest;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import java.io.IOException;
import java.util.List;
public class MapsActivity extends FragmentActivity {
// the Google Map object
private GoogleMap mMap;
private LocationManager locationManager;
private Location mCurrentLocation;
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
public Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
private double longitude = location.getLongitude();
private double latitude = location.getLatitude();
private LatLng STARTING_MARKER_POSITION =new LatLng(longitude, latitude);
private LatLng distanceFrom = STARTING_MARKER_POSITION;
// line will be drawn at the click event
private Polyline line=null;
// A Geocoder can transform a pair of latitude/longitude into a street address and viceversa.
// We'll use it in the listener
private static Geocoder geocoder=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// we set the layout for the Activity
setContentView(R.layout.activity_maps);
// the geocoder is instantiated for the first time
geocoder=new Geocoder(this);
// if there isn't a map, it will be created
setUpMapIfNeeded();
}
private GoogleMap.OnMapClickListener clickListener=new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(final LatLng pos) {
// this method is called when the user taps the map
// if a line already appears, it's removed
if (line!=null)
line.remove();
// a new line is created
line = mMap.addPolyline(new PolylineOptions()
.add(distanceFrom, pos)
.width(5) // width of the line
.color(Color.RED)); // line color
// call the converter object for geocoding invocation and distance calculation
new AddressConverter().execute(distanceFrom, pos);
}
};
#Override
protected void onResume() {
super.onResume();
// the availability of the GoogleMap will be checked before the Activity starts interacting with the user
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// the map is created only it has not been initialized
if (mMap == null) {
// the map is located in the layout
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// if a map exists, we proceed with initialization
if (mMap != null) {
setUpMap();
}
}
}
// Now it's time to configure the map. We can add markers, shapes, event handlers and so on
private void setUpMap() {
// the camera will be positioned according to the new coordinates
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(STARTING_MARKER_POSITION, 16));
// we choose the type of the map: Satellite in this case
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
// markerOptions describes the marker we want to place
MarkerOptions markerOptions=new MarkerOptions()
.position(STARTING_MARKER_POSITION)
.draggable(true);
// the marker has to be draggable as we'll move it
// the marker is rendered on the map
mMap.addMarker(markerOptions);
// we define the object to invoke when the marker is dragged
mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener()
{
#Override
public void onMarkerDragStart(Marker arg0)
{
// this method is called when the drag starts
// the operation we need is the cancellation of a preexisting line
if (line!=null)
line.remove();
}
#Override
public void onMarkerDragEnd(final Marker pos)
{
// we get the final position of the marker
distanceFrom=pos.getPosition();
}
#Override
public void onMarkerDrag(Marker arg0)
{
// operations performed during the movement. Nothing to do
}
});
// the callback to invoke is set
mMap.setOnMapClickListener(clickListener);
}
// we want to know which address corresponds to this location
// we use AsyncTask to perform slower operations on a separate thread
private class AddressConverter extends AsyncTask<LatLng,Void,String>
{
// The ProgressDialog window we'll show during the calculation
private ProgressDialog progress=null;
// this method is called before the background job starts. It works on the main thread
#Override
protected void onPreExecute() {
// ProgressDialog is shown
progress= ProgressDialog.show(MapsActivity.this,"Distance calculator","We are calcultating the distance...", true,false);
}
// this method works on a separate thread
// it performs geocoding operations to retrieve the address of the points and calculates the distance in meters between them
#Override
protected String doInBackground(LatLng... params) {
float[] distance=new float[1];
try {
// the Location class contains what we need to calculate distances
Location.distanceBetween(params[0].latitude,params[0].longitude,params[1].latitude,params[1].longitude,distance);
// geocoding operations
List<Address> fromResult=geocoder.getFromLocation(params[0].latitude,params[0].longitude,1);
List<Address> toResult=geocoder.getFromLocation(params[1].latitude,params[1].longitude,1);
// the message informs the user about the distance from the marker to the point selected with the click
// if we have got both the addresses, we use them to compose the message, otherwise we show only the distance
if (fromResult.size()>0 && toResult.size()>0)
{
return "The distance is " + Math.round(distance[0]) + " meters";
}
else
return "The distance is " + Math.round(distance[0]) + " meters";
}
catch (IOException e) {
return "The distance is " + Math.round(distance[0]) + " meters";
}
}
#Override
protected void onPostExecute(String message)
{
if (progress!=null)
progress.dismiss();
// The builder of the window is instantiated
AlertDialog.Builder builder=new AlertDialog.Builder(MapsActivity.this);
builder.setTitle("Distance");
builder.setMessage(message);
// the Alert dialog appears
builder.show();
}
}
// this method only formats the message with addresses
private String getAddressDescription(Address a)
{
String city=a.getLocality();
String address=a.getAddressLine(0);
return "'"+address+"' ("+city+")";
}
}
The error thrown
FATAL EXCEPTION: main
Process: com.example.matthewmcnabb.moyola, PID: 27349
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.matthewmcnabb.moyola/com.example.matthewmcnabb.moyola.MapsActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2515)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
at android.app.Activity.getSystemService(Activity.java:5259)
at com.example.matthewmcnabb.moyola.MapsActivity.<init>(MapsActivity.java:51)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1650)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
at android.app.ActivityThread.access$900(ActivityThread.java:172)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
I get thrown with the java.lang.IllegalStateException: System services not available to Activities before onCreate() error.
That is because you are trying to call methods inherited from Activity, like getSystemService(), from a field initializer. This will not work. You need to wait until onCreate(), and usually until after super.onCreate(), before calling methods like getSystemService().
Ive tried placing my code from the start in the onCreate() method but this doesn't work either.
In this sample app, I get the LocationManager in onCreate() of a fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
template=getActivity().getString(R.string.url);
mgr=
(LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
}
The same principle will hold with onCreate() of an activity.
When I try to place the (LocationManager)getSystemService(Context.LOCATION_SERVICE); in the onCreate() it requires a permission
You need to have a <uses-permission> element in the manifest for ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION, depending on whether you plan on using GPS_PROVIDER or NETWORK_PROVIDER.
On Android 6.0+, if your targetSdkVersion is 23 or higher, you need to implement runtime permissions, as those permissions are dangerous.
You try to get contexts and services in a constructor. This is wrong.
The constructor is executed when the object is created, before it is attached to the Android framework.
Just move member initialization to onCreate().
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
In my application I noticed these three things:
-The back button is enabled when going from one activity to another enabling the user to click on back to the original activity. The problem is I don't want the user to click on Back at a certain point in my application. I don't want to disable the back button completely in my application, only when one intent is called. How can I do that?
-I noticed something strange... when a toast notification pops up in my application all is well until I exit my application. When I exit my application, some of the toast notifications are residual and are popping outside of my application. Is there a reason for that? Did I miss something in the activity lifecycle to handle the cancellation of toasts at a certain point?
Lastly, this one is rather tough to solve. How do I lock my screen so that when the user rotates the device, that the activity doesn't not get called again and the asynctask can still resume without starting over again?
Thanks a lot for your time. Just curious why these things happen and what should I look into?
Here's my code:
//Main Activity.java
package com.example.Patient_Device;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.*;
public class MainActivity extends Activity {
//fields
private ProgressDialog progressBar;
private Context context;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_setup);
//Set the context
context = this;
//Initialize the start setup button and add an onClick event listener to the button
final Button start_setup_button = (Button) findViewById(R.id.start_setup_button);
start_setup_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Executes the AsyncTask
new RetrieveInfoTask().execute();
//Instantiates the intent to launch a new activity
Intent myIntent = new Intent(MainActivity.this, RetrieveInfoActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
public class RetrieveInfoTask extends AsyncTask<Void, Void, Void> {
//Called on the UI thread to execute progress bar
#Override
protected void onPreExecute() {
super.onPreExecute();
progressBar = new ProgressDialog(context);
progressBar.setIndeterminate(true);
progressBar.setCancelable(false);
progressBar.setMessage(MainActivity.this.getString(R.string.retrieve_info));
progressBar.show();
}
//Methods that retrieves information from the user device. This is performed in the Background thread
private void retrieveInfo() {
try {
//Reading the drawable resource line by line
String str="";
StringBuffer buf = new StringBuffer();
InputStream is = MainActivity.this.getResources().openRawResource(R.drawable.user_info);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is!=null) {
while ((str = reader.readLine()) != null) {
buf.append(str + "\n" );
}
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//doInBackground calls retrieveInfo() to perform action in Background
#Override
protected Void doInBackground(Void... params) {
retrieveInfo();
return null;
}
//When the background task is done, dismiss the progress bar
#Override
protected void onPostExecute(Void result) {
if (progressBar!=null) {
progressBar.dismiss();
}
}
}
}
//RetrieveInfoActivity.java
package com.example.Patient_Device;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.BatteryManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class RetrieveInfoActivity extends Activity {
private static String TAG = "RetrieveInfoActivity";
private Context context;
String fileLastSync = "09-18-2014 03:47 PM";
#Override
public void onCreate(Bundle savedInstanceState) {
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.retrieve_info);
//Once the new activity is launched, the setup is complete
Toast.makeText(getApplicationContext(), "Setup Complete!",
Toast.LENGTH_LONG).show();
//Gets the 'last synced' string and sets to datetime of the last sync
Resources resources = context.getResources();
String syncString = String.format(resources.getString(R.string.last_sync), fileLastSync);
//Dynamically sets the datetime of the last sync string
TextView lastSyncTextView = ((TextView) findViewById(R.id.last_sync) );
lastSyncTextView.setText(syncString);
//calls registerReceiver to receive the broadcast for the state of battery
this.registerReceiver(this.mBatInfoReceiver,new
IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent intent) {
//Battery level
int level = intent.getIntExtra("level", 0);
//Dynamically sets the value of the battery level
TextView batteryTextView = ((TextView) findViewById(R.id.battery) );
batteryTextView.setText("Battery Level: " + String.valueOf(level)+ "%");
//If the battery level drops below 25%, then announce the battery is low
//TODO: Add 25 to constants file.
if(level < 25) {
Toast.makeText(getApplicationContext(), "Low Battery!",
Toast.LENGTH_LONG).show();
}
//Plugged in Status
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
//Battery Status
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
//If the device is charging or contains a full status, it's charging
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
//If the device isCharging and plugged in, then show that the battery is charging
if(isCharging && plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB) {
Toast.makeText(getApplicationContext(), "Charging.." + String.valueOf(level)+ "%",
Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Unplugged!",
Toast.LENGTH_LONG).show();
}
}
};
#Override
public void onDestroy() {
try {
super.onDestroy();
unregisterReceiver(this.mBatInfoReceiver);
}
catch (Exception e) {
Log.e(RetrieveInfoctivity.TAG, getClass() + " Releasing receivers-" + e.getMessage());
}
}
}
//StartSetupActivity.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class StartSetupActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
//FragmentsActivity.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentsActivity extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.main, container, false);
}
}
First of all whenever you want to disable back press just override onBackPressed() method and remove super. like this:
#Override
public void onBackPressed() {
//super.onBackPressed();
}
Second you'r using application context to show toast. use activity context.
Toast.makeText(this or YourActivity.this, "Setup Complete!", Toast.LENGTH_LONG).show();
Third just add this attribute into your manifest class. This will avoid recrating your activity when orientation change
android:configChanges="orientation"
I'll answer these in order:
Back Button
You can override onBackPressed in your Activity and determine if you want to consume it or let Android process it.
#Override
public void onBackPressed()
{
// Set this how you want based on your app logic
boolean disallowBackPressed = false;
if (!disallowBackPressed)
{
super.onBackPressed();
}
}
Toasts
Toasts are enqueued with the Notification Manager. If you show multiple Toasts in a row, they get queued up and shown one at a time until the queue is empty.
Locking Orientation For Activity
Use android:screenOrientation="landscape" or android:screenOrientation="portrait" on your activity element in your manifest to lock the orientation.
I think that these questions should be asked separately, because the answer in detail to every item of your question is too long, but I hope this helps:
-The back button is enabled when going from one activity to another enabling the user to click on back to the original activity. The
problem is I don't want the user to click on Back at a certain point
in my application. I don't want to disable the back button completely
in my application, only when one intent is called. How can I do that?
You can override the onBackPressed on the activities you don't want the user to go back.
#Override
public void onBackPressed() {
//Leave it blank so it doesn't do anything
}
-I noticed something strange... when a toast notification pops up in my application all is well until I exit my application. When I exit my
application, some of the toast notifications are residual and are
popping outside of my application. Is there a reason for that? Did I
miss something in the activity lifecycle to handle the cancellation of
toasts at a certain point?
I think that the reason behind that is that toast go into a que, and are showed in order, even if the app is no longer visible.
Lastly, this one is rather tough to solve. How do I lock my screen so
that when the user rotates the device, that the activity doesn't not
get called again and the asynctask can still resume without starting
over again?
For this, you can use the following code in your manifest
android:configChanges="orientation|screenSize"/>
However this is NOT recommended by google, I suggest you read the following link to get a little more information on how to handle orientation changes:
http://developer.android.com/guide/topics/resources/runtime-changes.html
As a N00bie to android, I'm trying to build a simple map-app. I started out doing everything in the same class, but for obvious reasons that got out of hand, fast. So I wanted to split the class, with the base class as the main flow for the activity, and the subclass as the 'utility' class.
So I instantiate an Subclass object, and in the subclass's onCreate I start calling methods. These methods never run though. What am I doing wrong? As soon as I create the subclass object, the sub's onCreate should fire, no? And, is it even the smart way of doing this in a subclass, instead of a whole other class?
Thanks in advance!
Base class:
package com.example.TestMap;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
public class MyActivity extends Activity {
private GoogleMap mMap;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationClass locationClass = new LocationClass();
}
public void setMap(){
Log.i("TestMap", "setMap");
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
mMap.setMyLocationEnabled(true);
}
public void setCamera(Location location) {
Log.i("TestMap", "setCamera");
final LatLng locationLatLng = new LatLng( (location.getLatitude() ), location.getLongitude() );
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(locationLatLng) // Sets the center of the map to Mountain View
.zoom(17) // Sets the zoom
.bearing(90) // Sets the orientation of the camera to east
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
Subclass
package com.example.TestMap;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class LocationClass extends MyActivity implements LocationListener {
private LocationManager locationManager;
private String provider;
private List<String> providers;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TestMap", "LocationClass OnCreate");
GetProivder();
}
public void GetProivder (){
Log.i("TestMap", "LocationClass GetProivder");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(2);
provider = locationManager.getBestProvider(criteria, false);
providers = locationManager.getProviders(true);
Location location = locationManager.getLastKnownLocation(provider);
Log.i("TestMap", "providerlist = " + providers);
Log.i("TestMap", "getBestProvider = " + provider);
Log.i("TestMap", "Location = " + location);
if (location != null) {
Log.i("TestMap", "Provider " + provider + " has been selected.");
super.setCamera(location);
super.setMap();
} else {
Log.i("TestMap", "location is null.");
}
}
#Override
public void onLocationChanged(Location location) {
Log.i("TestMap", "onLocationChanged");
}
#Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.i("TestMap", "onStatusChanged");
}
#Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Log.i("TestMap", "onProviderDisabled");
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
Your code
LocationClass locationClass = new LocationClass(); does not create it. It just makes an object of it, but it does not tie to the lifecycle and call the methods.
You need to start it with an intent to make it appear (and take the entire screen based on skimming your code). Android will fire the appropriate method calls when you do so.
Like this:
Intent intent = new Intent(this, LocationClass.class);
startActivity(intent);
More information can be found here: http://developer.android.com/training/basics/firstapp/starting-activity.html#StartActivity
It must be defined in your manifest too or it will crash. There are other noticeable oddities things in your code, do you want LocationClass to extend MyActivity and not Activity? Your LocationClass also does not call setContentView() in onCreate, so you're not going to see a UI (as far as I can tell), unless you wanted it through the extends part.
EDIT :
If you extend subclass and put this intent code in onCreate, you're probably going to crash, as it will call super() in MyActivity (calling onCreate() again as it's the superclass), and will keep making more intents to start the activity. You should not subclass MyActivity if that's what the 'parent' class is.
You should only subclass Activity or a global parent activity (e.g. in my project right now, I extend SpiceActivity, as they all use common components related to Spice).
The subclass' onCreate method should be called when it is created by Android (see Understanding the Lifecycle Callbacks), which for example happens when an Intent to that Activity is issued.
Edit: I just saw #Mgamerz answer and realised that line in your superclass Activity was where you were trying to make Android create the subclass Activity. This next paragraph is somewhat irrelevant now, but note that you do still need to add the subclass activity to the manifest file.
Are you sure you're application is actually starting the subclass Activity, or is it still starting the superclass Activity? You might have to have a look at your project's "AndroidManifest.xml" file and check that there's an <activity /> element corresponding to the subclass Activity.
I think splitting the class into a superclass and a subclass is sensible if the superclass has functionality which can/will be re-used by multiple subclass Activities. For example you might have subclass activities like DirectionsActivity and SearchActivity which have some common map-related activity provided by their superclass MapActivity. Even if you have only one subclass Acitivty now, it may still make sense to have a superclass and a subclass if you think you're likely to write additional map-related activities later on. I my opinion it's not sensible to split the class into a superclass and a subclass just because the single class was getting too long. If you do just want a helper class and you don't expect to have more MapActivity subclasses in the future, you could make a MapUtils class in the same package as the Activity class, which would define some static helper methods. For example, you could put your GetProvider method into such a helper class. Sketch example (note the package-private access of MapHelper):
class MapHelper {
static Location getProvider() {
// ...
return location;
}
// Other helper methods here
}
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// ...
setCamera(MapHelper.getProvider());
setMap();
}
// Other activity methods here
}
I am trying to write a simple app that takes the users current location and notifies them when they come near this location later. In my code I have it set up so that when the app starts it starts requesting location updates. Then when the button is pressed it takes the last known location and creates a proximity alert around that. my problem however is that when i press the button on both the emulator and the real phone it quickly crashes and i cant seem to find a reason why. is there an infinite loop i am some how missing? if there is a way to see the error messages from my phone that would also be very helpful.
Here is my code:
MainActivity.java
package com.YdidApplications.shotgun;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener
{
private ImageButton flagButton;
LocationManager lm;
double lat=0,long1=0; //Defining Latitude & Longitude
float radius=5; //Defining Radius
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm=(LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(lm.GPS_PROVIDER,
10000, //Update every 10 sec
10, (LocationListener) this);
flagButton = (ImageButton) findViewById(R.id.FLAG);
onFlagPress();
}
public void onFlagPress()
{
flagButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
location();
}
});
}
public void location()
{
lm=(LocationManager) getSystemService(LOCATION_SERVICE);
lm.getLastKnownLocation(LOCATION_SERVICE);
Intent i= new Intent("com.adnan.proximityalert"); //Custom Action
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), -1, i, 0);
lm.addProximityAlert(lat, long1, radius, -1, pi);
}
#Override
public void onLocationChanged(Location location) {
lat= location.getLatitude();
long1 = location.getLongitude();
Toast.makeText(null, lat+", "+long1, 0);
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
ProximityReciever.java
package ProximityReciever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.widget.Toast;
/*This is the Reciever for the Brodcast sent, here our app will be notified if the User is
* in the region specified by our proximity alert.You will have to register the reciever
* with the same Intent you broadcasted in the previous Java file
*
* #Author: Adnan A M
*/
public class ProximityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
// The reciever gets the Context & the Intent that fired the broadcast as arg0 & agr1
String k=LocationManager.KEY_PROXIMITY_ENTERING;
// Key for determining whether user is leaving or entering
boolean state=arg1.getBooleanExtra(k, false);
//Gives whether the user is entering or leaving in boolean form
if(state){
// Call the Notification Service or anything else that you would like to do here
Toast.makeText(arg0, "Welcome to my Area", 600).show();
}else{
//Other custom Notification
Toast.makeText(arg0, "Thank you for visiting my Area,come back again !!", 600).show();
}
}
}
Any advise would be greatly appreciated.
Thanks,
Logan