2012-09-05 122 views
0

我试图在后台运行应用程序,但是当应用程序立即启动时,应用程序已关闭(未出现错误)。我在主要活动中使用了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(); 
    } 

} 

为服务intiate活性 代码的代码:

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" /> 

有人遇到过吗?

回答

0

使用Service代替Activity做背景长远的工作

0

您只能在GUI上使用的setText等操作从主界面线程。您正尝试从您已开始的后台线程完成此操作。

您必须实现AsyncTask函数onPostExecute并从那里修改您的GUI。

+0

我曾尝试只募集来自asynk任务的敬酒,但活动立即关闭,因此吐司没有升起。 –

0

出来的内存,搭上LOG onDestoy()方法

0

有你的android manifest文件中提及的接收器, 添加这串代码在你manifest file

<receiver 
     android:name=".MyBroadcastReceiver" > 
     <intent-filter android:priority="1000"> 

      <action /> 

     </intent-filter> 

    </receiver> 
相关问题