2012-08-01 175 views
2

這是我的Android應用程序的代碼。 MainActivity.java應用程序在手機的重新啓動時崩潰

public class MainActivity extends Activity { 

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters 
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000; // in Milliseconds 

    protected LocationManager locationManager; 

    protected Button retrieveLocationButton; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Log.d("MyTag", "first"); 
     retrieveLocationButton = (Button) findViewById(R.id.button1); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     locationManager.requestLocationUpdates(
       LocationManager.GPS_PROVIDER, 
       MINIMUM_TIME_BETWEEN_UPDATES, 
       MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
       new MyLocationListener() 
     ); 

    retrieveLocationButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showCurrentLocation(); 
       Log.d("MyTag", "Second"); 
      } 
    });   

    }  

    protected void showCurrentLocation() { 

     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     Log.d("MyTag", "Third"+location); 
     if (location != null) { 
      String message = String.format(
        "Current Location \n Longitude: %1$s \n Latitude: %2$s", 
        location.getLongitude(), location.getLatitude() 
      ); 
      Toast.makeText(MainActivity.this, message, 
        Toast.LENGTH_LONG).show(); 
      Log.d("MyTag", "Fifth"); 
     } 

    } 

    private class MyLocationListener implements LocationListener { 

     public void onLocationChanged(Location location) { 
      String message = String.format(
        "New Location \n Longitude: %1$s \n Latitude: %2$s", 
        location.getLongitude(), location.getLatitude() 
      ); 
      Log.d("MyTag", "sixth"); 
      Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show(); 
     } 

     public void onStatusChanged(String s, int i, Bundle b) { 
      Toast.makeText(MainActivity.this, "Provider status changed", 
        Toast.LENGTH_LONG).show(); 
     } 

     public void onProviderDisabled(String s) { 
      Toast.makeText(MainActivity.this, 
        "Provider disabled by the user. GPS turned off", 
        Toast.LENGTH_LONG).show(); 
     } 

     public void onProviderEnabled(String s) { 
      Toast.makeText(MainActivity.this, 
        "Provider enabled by the user. GPS turned on", 
        Toast.LENGTH_LONG).show(); 
     } 

    } 

} 

BootReceiver.java

public class BootReceiver extends BroadcastReceiver  { 
    @Override 
    public void onReceive(Context context, Intent intent){ 
     context.startService(new Intent(context,MainActivity.class)); 
     Log.d("kkkkkkkk","bootReceive"); 
    } 
} 

AndroidMainfest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.pragmatic.pragmaticgps" 
    android:versionCode="1" 
    android:versionName="1.0" > 

<uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
     <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />  
     <uses-permission android:name="android.permission.INTERNET" />  
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 
<uses-permission android:name="android.permission.REBOOT"/> 

    <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> 
     <service android:name=".MainActivity"></service> 

     <receiver 
      android:name=".receiver..MainActivity" 
      android:enabled="true" > 
      <intent-filter> 
       <action android:name="android.intent.action.REBOOT" /> 
      </intent-filter> 
     </receiver> 


     <receiver android:name=".BootReceiver"> 
      <intent-filter > 
       <action android:name="android.intent.action.BOOT_COMPLETED"/> 
      </intent-filter> 
     </receiver> 


    </application> 

</manifest> 

我想這個應用獲取GPS cordinates即經度/緯度,並通過敬酒顯示它( )我也希望只要手機重新啓動,這個應用程序就會自動啓動,並在24x7的背景下運行,但是在切換時在真正的Android手機上運行在電話上,應用程序的craches和彈出窗口出現要求強制關閉它。代碼中出了什麼問題?對不起,直接詢問錯誤,但我對Android瞭解不多。
謝謝。

+0

上傳您的崩潰Logcat的更多信息。 – Lucifer 2012-08-01 06:14:05

回答

2

感覺就像錯誤就出在這裏:

context.startService(new Intent(context,MainActivity.class)); 

可以啓動MainActivityService,因爲它不是一個Service,這是一個Activity。要啓動Activity你應該叫

context.startActivity(new Intent(context,MainActivity.class))

而且,在這種情況下,這將是更好地組織MainActivityService,因爲這是一個非常糟糕的主意直接從BroadcastReceiver啓動Activity。希望這可以幫助。

+0

通過組織MainActivity作爲服務,你的意思是我必須在mainfest.xml中寫下這個標籤「」? – Nagri 2012-08-01 06:47:24

+0

@HussainNagri,不僅應該擴展Service而不是Activity。我相信這篇文章會幫助你:http://developer.android.com/guide/components/services.html – Egor 2012-08-01 07:10:44