gpt4 book ai didi

android - 手机重启时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 00:37:26 25 4
gpt4 key购买 nike

这是我的 Android 应用程序的代码。主 Activity .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 坐标,即纬度/经度,并通过 toast 显示它(它确实如此)。我还希望每当手机重新启动时,这个应用程序应该自动启动并在后台 24x7 运行,但在真实的Android 手机当我打开手机时,应用程序崩溃并弹出一个要求强制关闭它的弹出窗口。代码有什么问题?很抱歉直接问这个错误,但我对 Android 了解不多。
谢谢。

最佳答案

感觉问题出在这里:

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

您可以将 MainActivity 作为 Service 启动,因为它不是 Service,而是 Activity。要启动 Activity,您应该调用

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

此外,在这种情况下,最好将 MainActivity 组织为 Service,因为直接启动 Activity 是一个非常糟糕的主意来自 BroadcastReceiver。希望这会有所帮助。

关于android - 手机重启时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11753486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com