アプリケーションをバックグラウンドで実行しようとしていますが、アプリケーションがすぐに起動すると、アプリケーションは閉じられます(エラーは発生しません)。メインアクティビティでasynkタスクを使用しました。
コード:
package com.android.trace;
public class LocationStat extends Activity {
double logi;
double lat;
long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Millisecon
Location loc;
LocationManager manager;
TextView t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new MyLocationAsyncTask().execute();
}
public void onStart() {
super.onStart();
new MyLocationAsyncTask().execute();
}
private class MyLocationAsyncTask extends AsyncTask<Void, Location, Void> implements LocationListener {
//private Location l;
//location management variables to track and maintain user location
@Override
protected Void doInBackground(Void... arg0) {
t = (TextView) findViewById(R.id.text);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
new MyLocationAsyncTask());
return onLocationChanged();
}
//this method is never executed i dont know why...?
public Void onLocationChanged() {
if (manager != null) {
LocationStat l = new LocationStat();
loc = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
lat = loc.getLatitude();
logi = loc.getLongitude();
t.setText(" Your Location :\nlongitude:" + logi + "\nlatitude: " + lat); //Log.d("Your Location", ""+latLocation);
l.webcall(logi, lat);
}
return null;
}
public void onLocationChanged(Location location) {
onLocationChanged();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
public void webcall(double logi, double lat) {
InputStream is = null;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("logitude", Double.toString(logi)));
nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(lat)));
//http post
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/location.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Toast.makeText(LocationStat.this, "Error in http connection " + e.toString(), Toast.LENGTH_LONG).show();
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Toast.makeText(LocationStat.this, result, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(LocationStat.this, "Error converting result " + e.toString(), Toast.LENGTH_LONG).show();
}
}
アクティビティコードを開始するためのサービスのコード:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
String tag="TestService";
@Override
public void onCreate() {
Intent dialogIntent = new Intent(this, LocationStat.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(dialogIntent);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
起動時にサービスを開始するために使用される放送受信機は
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
startServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(startServiceIntent);
}
}
アクティビティ、サービス、およびブロードキャストレシーバーのマニフェスト権限は次のとおりです
<service android:enabled="true" android:name=".MyService">
<intent-filter >
<action android:name="com.android.trace.MyService"/>
</intent-filter>
</service>
<receiver android:name="com.android.trace.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity
android:name=".LocationStat"
android:label="@string/title_activity_location_stat" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
同じことを経験している人はいますか?