I have a LocationActivity that gets the GPS location from the Google API. I need to use this data in my MainActivity. I am using startActivityForResult to accomplish this, but my LocationActivity never executes and my app crashes when I try to access the data.
Here is are some relevant snippets from MainActivity.java:
public void onUpdateClick(View v) { //button click handler which starts the process
Intent i = new Intent(MainActivity.this, LocationActivity.class);
startActivityForResult(i, GPS_DATA_REQUEST);
byte[] data = latitude.getBytes(); //app crashes here because latitude is null
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GPS_DATA_REQUEST) {
if(resultCode == Activity.RESULT_OK){
latitude=data.getStringExtra("latitude");
}
}
}
And here are relevant snippets from LocationActivity.java:
#Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest,this);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
latitude = String.valueOf(mLastLocation.getLatitude());
}
Intent returnIntent = new Intent();
returnIntent.putExtra("latitude",latitude);
setResult(LocationActivity.RESULT_OK, returnIntent);
finish();
}
And my Android Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/label_broadcast">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LocationActivity">
</activity>
</application>
I have referred to many threads and the Android activity/intent documentation but have not been able to solve this. Any help/suggestions would be appreciated. I have tried to provide only the relevant information. Thanks!
Move the code in LocationActivity from onConnected to onStart. startActivityForResult invokes the onStart lifecycle method.
You cant used
byte[] data = latitude.getBytes();
At time of activity start latitude will be null
Update your code with this.
public void onUpdateClick(View v) { //button click handler which starts the process
Intent i = new Intent(MainActivity.this, LocationActivity.class);
startActivityForResult(i, GPS_DATA_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GPS_DATA_REQUEST) {
if(resultCode == Activity.RESULT_OK){
latitude=data.getStringExtra("latitude");
byte[] data = latitude.getBytes();
}
}
}
It not looks good.. Once it is connected to location service it looks like you finish second activity.. Then it will kill your second activity and again resumes your first activity. Here, you need to change logic in LocationActivity
You can try change LocationActivity.java
Intent returnIntent = new Intent(LocationActivity.this,MainActivity.class);
returnIntent.putExtra("latitude",latitude);
setResult(LocationActivity.RESULT_OK, returnIntent);
Problem is on LocationActivity class on line
setResult(LocationActivity.RESULT_OK, returnIntent);
Actually you need
setResult(GPS_DATA_REQUEST, returnIntent);
You can try this. I hope you will get you solution.
MainActivity.java
public void onUpdateClick(View v) { //button click handler which starts the process
Intent i = new Intent(MainActivity.this, LocationActivity.class);
startActivityForResult(i, GPS_DATA_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GPS_DATA_REQUEST) {
if(resultCode == Activity.RESULT_OK){
latitude=data.getStringExtra("latitude");
if(latitude != null)
byte[] data = latitude.getBytes();
else
//do request again for latitude.
}
}
}
You need to set result in you LocationActivity.java
Intent returnIntent = new Intent(LocationActivity.this, MainActivity.class);
returnIntent.putExtra("latitude",latitude);
setResult(RESULT_OK, returnIntent);
I had the same problem.
Then I realized that I've initialized and declared my views before the second activity's OnCreate method is invoked. Putting declaration into OnCreate as shown below solved the problem.
public class Activity2 extends AppCompatActivity{
// View declaration, put your views here
TextView mTextViewExample;
EditText mEditTextExample;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
setTitle(R.string.title_activity2);
// View initialization, put your views here
mTextViewExample = (TextView) findViewById(R.id.textViewExample);
mEditTextExample = (EditText) findViewById(R.id.editTextExample);
}
}
Related
I want the first LoginActivity.java page to open in my project, but AnasayfaActivity.java opens. I couldn't solve the problem. I use Android Studio. I am learning Android, and I would be glad if you help. I shared LoginActivity and AndroidManifest pages. When the application is opened for the first time, the Home Activity opens, when I go back, it goes to the LoginActivity.java page.
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.geziproject">
>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Geziproject">
<activity android:name=".AnasayfaActivity"/>
<activity android:name=".MainActivity2" />
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/Theme.Geziproject.NoActionBar">
</activity>
<activity android:name=".bos" />
<activity android:name=".kayitol" />
<activity android:name=".kullanicigiris" />
</application>
</manifest>
LoginActivity.java:
public class LoginActivity extends AppCompatActivity {
private Button signInButton;
private GoogleSignInClient mGoogleSignInClient;
private FirebaseAuth mAuth;
private Button signout;
Button btngiris;
Button btnkayit;
private EditText txtad;
private EditText txtemail;
private EditText txtsifre;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button btngiris = findViewById(R.id.btngiris);
Button btnkayit = findViewById(R.id.btnkayit);
txtemail = findViewById(R.id.txtemail);
txtsifre = findViewById(R.id.txtsifre);
signInButton = findViewById(R.id.signin);
mAuth = FirebaseAuth.getInstance();
// signout = findViewById(R.id.sign_out);
btngiris.setOnClickListener(v -> {
String email=txtemail.getText().toString();
String pwd= txtsifre.getText().toString();
if(email.isEmpty()){
txtemail.setError("Lütfen email giriniz");
txtemail.requestFocus();
}
else if(pwd.isEmpty()){
txtsifre.setError("Lütfen şifre giriniz");
txtsifre.requestFocus();
}
else if(email.isEmpty() && pwd.isEmpty())
{
Toast.makeText(LoginActivity.this,"Bu alanlar boş bırakılamaz",Toast.LENGTH_LONG).show();
}
else if(!(email.isEmpty() && pwd.isEmpty())){
mAuth.signInWithEmailAndPassword(email,pwd).addOnCompleteListener(LoginActivity.this, task -> {
if(!task.isSuccessful()){
Toast.makeText(LoginActivity.this,"Giriş başarısız ,tekrar deneyiniz",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(LoginActivity.this,"Giriş başarılı",Toast.LENGTH_LONG).show();
txtemail.setText("");
txtsifre.setText("");
startActivity(new Intent(LoginActivity.this,AnasayfaActivity.class));
}
});
}
else{
Toast.makeText(LoginActivity.this,"Hata oluştu",Toast.LENGTH_LONG).show();
}
});
btnkayit.setOnClickListener(v -> {
Intent i= new Intent(LoginActivity.this,kayitol.class);
startActivity(i);
});
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken("1088981466528-8pkjha8350r2uniqg2425nv5itvgpvr7.apps.googleusercontent.com")
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
signInButton.setOnClickListener(v -> signIn());
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
startActivity(new Intent(LoginActivity.this, AnasayfaActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
private void signIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, 100);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
//handleSignInResult(task);
if (task.isSuccessful()) {
String s = "Google sign in Successful";
displayToast(s);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
Toast.makeText(getApplicationContext(), "giriş başarılı", Toast.LENGTH_LONG).show();
if (account != null) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(getApplicationContext(), AnasayfaActivity.class);
startActivity(intent);
}
}
});
}
} catch (ApiException e) {
e.printStackTrace();
}
}
}
}
private void displayToast(String s) {
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
As you set the launch activity in mainfest to LoginActivity, the app starts normally with the LoginActivity, but before the LoginActivity is resumend (i.e. shown on the screen) you added a condition in onCreate() method to launch the AnasayfaActivity as below:
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
startActivity(new Intent(LoginActivity.this, AnasayfaActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
If the user is not null (i.e. already logged-in), then the app goes directly to AnasayfaActivity activity without showing the LoginActivity to the user.
And the reason when you go back; the app go to the LoginActivity, because the LoginActivity is still in the back stack. If you want to remove the LoginActivity from the back stack then add Intent.FLAG_ACTIVITY_CLEAR_TOP to the intent:
startActivity(new Intent(LoginActivity.this, AnasayfaActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
I'm new to Java and Android so some errors might be basic.
I have created two classes: Rdiet_Main.java and Rdiet_Zxing.java. Everything used to be in the same class but I wanted to keep one functionality per class.
When the scan button is pressed this happens in Rdiet_Main:
Rdiet_Zxing zxScan = new Rdiet_Zxing();
zxScan.acquireBarcode(this);
Then it goes in the Rdiet_Zxing:
public void acquireBarcode(Rdiet_Main t){
IntentIntegrator scanBarcodeIntegrator = new IntentIntegrator(t);
scanBarcodeIntegrator.initiateScan();
}
public String onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanBarcodeResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if(scanBarcodeResult != null){
String strResult;
strResult = scanBarcodeResult.getContents();
return strResult;
}
else{
return null;
}
}
I want to send the barcode to Rdiet_Main. How should I change my code to make it possible?
EDIT :
I now use startActivityForResult. However the scanner isn't launched when the button is pressed and the app just crashes (Unable to find explicit Activity Class in AndroidManifest.xml) . I think Rdiet_Zxing needs to be linked to a CaptureActivity or something similar.
Here's what's inside my AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Rdiet_Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
EDIT2 :
Updated Rdiet_Zxing.java:
public class Rdiet_Zxing extends Activity{
public static final int SUCCESS_RETURN_CODE = 1;
public static final int FAILURE_RETURN_CODE = 2;
public void acquireBarcode(Rdiet_Main t){
IntentIntegrator scanBarcodeIntegrator = new IntentIntegrator(t);
scanBarcodeIntegrator.initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanBarcodeResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if(scanBarcodeResult != null){
String strResult;
strResult = scanBarcodeResult.getContents();
//SOL1
Intent intentResult = new Intent();
Bundle b = new Bundle();
b.putString("myResult", strResult);
intentResult.putExtras(b);
setResult(SUCCESS_RETURN_CODE, intentResult);
finish();
//-------------
}
else{
setResult(FAILURE_RETURN_CODE, null);
finish();
}
}
}
Updated Rdiet_Main.java:
//Scan button
Intent i = new Intent(Rdiet_Main.this, Rdiet_Zxing.class);
startActivityForResult(i, SUB_ACTIVITY_REQUEST_CODE);
//ScanResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SUB_ACTIVITY_REQUEST_CODE) {
Bundle b = data.getExtras();
strBarcode = b.getString("myResult");
tbxAPIbarcode.setText(strBarcode);
}
}
create a callback like below
public interface CallbackToMain {
void result(String scanvalue);
}
Implement it on your Redit_Main Activity.
create object of Rdiet_Zxing class inside Redit_Main and pass interface reference.
Rdiet_Zxing zxScan = new Rdiet_Zxing(this); // this refered to callback interface.
zxScan.acquireBarcode();
Inside Redit_Zxing do things like below.
CallbackToMain callback;
public void Rdiet_Zxing(CallbackToMain callback){
this.callback=callback;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callback.result("your scanned value");
}
You can do all these events in the same class or you can use 'sub activity'.
Edit: Here is example
In your main activity, when you press the button:
Intent i = new Intent(Rdiet_Main.this, Rdiet_Zxing.class);
startActivityForResult(i, SUB_ACTIVITY_REQUEST_CODE);
Then, you will start the scanner. When you get your result:
public String onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanBarcodeResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if(scanBarcodeResult != null){
String strResult;
strResult = scanBarcodeResult.getContents();
//Add these lines
Intent intent = new Intent();
Bundle b = new Bundle();
b.putString("myResult", strResult);
intent.putExtras(b);
setResult(SUCCESS_RETURN_CODE, intent);
finish();
//-------------
return strResult;
}
else{
return null;
}
}
Also in scanner activity add this as global:
public static final int SUCCESS_RETURN_CODE = 1;
And in main activity:
private static final int SUB_ACTIVITY_REQUEST_CODE = 100;
Finally, to get your result in main:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SUB_ACTIVITY_REQUEST_CODE) {
Bundle b = data.getExtras();
String result = b.getString("myResult");
}
}
Edit2:
When you create a new activity it must be added to your manifest but if it's not, you can add like this,
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Rdiet_Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- added here -->
<activity android:name=".Rdiet_Zxing"></activity>
</application>
Hope it helps.
i am in real trouble here. Trying to get lat and lon with a background service every 3 sec but i am only able to get some data written when i click send lat and lon in extended controls of the emulator , so both phone and emulator are not working. Here is my code below, it would be awesome if someone could help me. Thanks!
Service
public class GPSService extends Service {
private static final String TAG = "GpsService";
private LocationListener locationListener;
private LocationManager locationManager;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Intent i = new Intent("location_update");
i.putExtra("latExtra",location.getLatitude());
i.putExtra("lonExtra",location.getLongitude());
sendBroadcast(i);
Log.i(TAG, "onLocationChanged: extras lat lon"+location.getLatitude()+" "+location.getLongitude());
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
Log.i(TAG, "onProviderDisabled: DISABLED");
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
locationManager =(LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Criteria c = new Criteria();
String provider =locationManager.getBestProvider(c,true);
Log.i(TAG, "onCreate: bestProvider "+provider);
//noinspection MissingPermission
locationManager.requestLocationUpdates(provider,2000,0,locationListener);
}
#Override
public void onDestroy() {
super.onDestroy();
if (locationManager != null){
Log.i(TAG, "onDestroy: Location manager nije null i brisem");
//noinspection MissingPermission
locationManager.removeUpdates(locationListener);
}
}
}
MainActivity
private final String TAG = "Main";
...
BroadcastReceiver broadcastReciever;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//setStatusBarTranslucent(false);
if(!runtimePermisions()){
startLocationUpdate();}
...
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//stopService();
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//REQUEST PERMISSION
Log.i(TAG, "onClick: NO PERMISION");
} else {
Log.i(TAG, "onClick: got permision");
}
...
}
public void startLocationUpdate(){
Intent i = new Intent(this,GPSService.class);
startService(i);
Log.i(TAG, "startLocationUpdate: Pokrenuo sam service");
}
#Override
protected void onResume() {
super.onResume();
if (broadcastReciever == null){
broadcastReciever = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
lat = (Double) intent.getExtras().get("latExtra");
lon = (Double) intent.getExtras().get("lonExtra");
Log.i(TAG, "onReceive: lat lon "+lat+" "+lon);
}
};
}
registerReceiver(broadcastReciever,new IntentFilter("location_update"));
}
#Override
protected void onDestroy() {
super.onDestroy();
if (broadcastReciever!=null){
unregisterReceiver(broadcastReciever);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100) {
if (grantResults [0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
startLocationUpdate();
}else{
runtimePermisions();}
}
}
private boolean runtimePermisions() {
if (Build.VERSION.SDK_INT >= 23 &&ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
},100);
return true;
}
return false;
}
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.digiart.yoweather">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SettingsActivity"
android:theme="#style/SettingsTheme"></activity>
<service android:name=".Gps.GPSService"/>
</application>
</manifest>
Again... Any help would be great! THANKS :D
As K. Sopheak said, it can take a while to get the location. From the documentation:
It may take a while to receive the first location update. If an immediate location is required, applications may use the getLastKnownLocation(String) method.
So, you could try getting the last known location using getLastKnownLocation(String) when the service is started and, assuming it exists, broadcast it in the same way you would a location update. Bear in mind, though, that the last known location could be out of date. Depending on what you're using the location for this may or may not be acceptable.
Also, as an aside, a couple of thoughts:
You said 3 seconds, but the code uses 2,000 milliseconds - was that just a typo?
The frequency of location updates is a minimum time - you are not guaranteed to get updates that often. As per the documentation:
The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications.
Is there any particular reason you need location updates at such a high frequency? Obtaining a location can be battery intensive, particularly given that you are requesting FINE as well as COARSE location permissions, so requesting it so frequently could place an enormous drain on device battery life. This is particularly so given that the code is running in a service and will therefore continue to run even when the application is in the background or the user is in an activity which does not require location data. Again, from the documentation:
Choosing a sensible value for minTime is important to conserve battery life. Each location update requires power from GPS, WIFI, Cell and other radios. Select a minTime value as high as possible while still providing a reasonable user experience. If your application is not in the foreground and showing location to the user then your application should avoid using an active provider (such as NETWORK_PROVIDER or GPS_PROVIDER), but if you insist then select a minTime of 5 * 60 * 1000 (5 minutes) or greater. If your application is in the foreground and showing location to the user then it is appropriate to select a faster update interval.
Google recommends using the Google Play services location APIs instead of the Android framework location APIs:
The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.
I'm sure this is a failure in my understanding of intents but I have an ExpandableListView of items and when I click on an item it launches the first one OK but for each time after that it only launches the first one again no matter which on I click on. The request debugs as OK but the received intent always debugs as if the first one was sent. After a half a day stuck on it and Google failing me, I need some help.
Activity #1 Mainfest
<activity
android:name="com.h17.gpm.ActivityToDoList"
android:launchMode="singleInstance"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.h17.gpm.TODO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Activity #1 Code
Intent launch = new Intent(ActivityToDoList.this, ActivityToDoEdit.class);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
launch.putExtra("Action._ID", a.get_ID());
Log.d("ACTIVITYLAUNCHTEST", "Launch["+a.get_ID()+"]");
startActivity(launch);
Activity #2 Mainfest
<activity
android:name="com.h17.gpm.ActivityToDoEdit"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.h17.gpm.TODO.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Activity Code #2
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todo_edit);
Intent i = getIntent();
Bundle extras = null;
if(i != null)
extras = i.getExtras();
if (extras != null){
action_id = extras.getLong("Action._ID");
Log.d("ACTIVITYLAUNCHTEST", "Receive["+action_id+"]");
}
}
I've read from other posts that getIntent returns the first Intent so also tried
#Override
protected void onNewIntent(Intent intent){
Bundle extras = null;
if(intent != null)
extras = intent.getExtras();
if (extras != null){
action_id = extras.getLong("Action._ID");
Log.d("ACTIVITYLAUNCHTEST", "Receive New Intent["+action_id+"]");
}
setIntent(intent);
}
I've also tried a lot of combinations of Intent Flags and Launch Modes in the Manifest but for the life of me the first time always comes up as
Launch[1]
Receive[1]
and the second time
Launch[2]
Receive[1]
and from then on no matter what value I send the activity launches with the first value, 1 and the onNewIntent never seems to fire.
The complete function that generates the intent
private void loadLists(){
ExpandableListView expandableList = (ExpandableListView) findViewById(R.id.expandableListViewToDoLists);
expandableList.setClickable(true);
adapter = new ActionListsExpandableAdapter(getApplicationContext());
expandableList.setAdapter(adapter);
expandableList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Action a = (Action) parent.getExpandableListAdapter().getChild(groupPosition, childPosition);
if (startedForResult){
Intent data = new Intent();
data.putExtra("Action._ID", a.get_ID());
data.putExtra("Action.SUBJECT", a.getSUBJECT());
setResult(RESULT_OK, data);
finish();
}else{
ActionList al = (ActionList) parent.getExpandableListAdapter().getGroup(groupPosition);
Intent launch = new Intent(ActivityToDoList.this, ActivityToDoEdit.class);
launch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
launch.putExtra("Action._ID", a.get_ID());
Log.d("ACTIVITYLAUNCHTEST", "Launching activity with intent for Action ID ["+a.get_ID()+"]");
launch.putExtra("ActionList._ID", al.get_ID());
launch.putExtra("ActionList.position", childPosition);
startActivity(launch);
}
return false;
}
});
}
May be this is a good reference for you.
To me it seems like onNewIntent (with FLAG_ACTIVITY_SINGLE_TOP) like onCreate will be called if app is not destroyed. Hope is helpful to you. It works for me in my case.
For further reference see
Android: get most recent intent
Right now I am getting a force close on my android emulator.
Upon finishing this app, I will want to put a custom field in instead of just test, but for now I just want test to show up from the http activity.
Any help would be great!
MainActivity:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.main.MESSAGE";
/*#SuppressLint("ParserError")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}*/
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private Button searchBtn;
#Override
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.activity_main);
searchBtn = (Button) findViewById(R.id.button1);
searchBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(null, http.class);
startActivity(intent);
}
});
}
}
Http:
public class http extends Activity {
public http(){
httpMethod();
}
public void httpMethod(){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://api.site.com/api/");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
;
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
String test = "hello";
TextView myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText(test);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
Manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.main.DisplayMessageActivity"/>
<activity android:name="com.example.main.http"/>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
</manifest>
There are whole lot of issues in the code:
1) Intent intent = new Intent(null, http.class);
Use first parameter as MainActivity.class instead of null
2) httpActivity should have onCreate (or) onResume life cycle activity methods to create activity for startActivity
Not but the least, please spend some time on reading documentation and doing example programs instead of just type-in something and post on SO. By going through all your questions it is something like SO community did your app for you.
You have to initialize Intent like this
Intent intent = new Intent(MainActivity.this, http.class);
You need to pass Context as first parameter not null.
start as:
searchBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, http.class);
startActivity(intent);
}
});
instead of passing null as First parameter in Intent Constructor
for more information see here
http://developer.android.com/reference/android/content/Intent.html
I trully advice you to read some Android basics beacause you have some issues in the code:
You have a null context when you're initializing the intent at the button's listener. You should have: Intent intent = new Intent(getApplicationContext(), http.class); or Intent intent = new Intent(MainActivity.this, http.class);
You need to create your ativity and set it's content. You must override at least the onCreate method.
It's not so important, but its a good practice to write code that anyone might understand instead of write code for the machine! I'm telling this because you have *activity_main* sml file where you define your main activity layout and menu. I suggest you to refractor these file names to something like main.xml, for the layout, and *main_mnu.xml*.