当我在我的物理设备(huawei.Android版本4.4.2)上运行应用程序时,它启动良好但挂在启动画面上。当我使用模拟器(Nexus 5 API 24)时,它会启动启动画面然后进入登录活动屏幕。可能是什么问题?
我的启动活动
public class SplashActivity extends AppCompatActivity {
GPSTracker gpsTracker;
protected static final int REQUEST_CHECK_SETTINGS = 0x1;
boolean permission=false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getDeviceToken();
getpermission();
gpsTracker = new GPSTracker(this, this);
permission= SharedPreferenceWriter.getInstance(this).getBoolean(SharedPreferenceKey.permission_granted_location,false);
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (statusOfGPS) {
if (permission) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (SharedPreferenceWriter.getInstance(SplashActivity.this).getBoolean(SharedPreferenceKey.currentLogin, false)) {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
} else {
startActivity(new Intent(SplashActivity.this, Login.class));
finish();
}
}
}, 2000);
} //else
//Toast.makeText(SplashActivity.this,"permission not granted",Toast.LENGTH_LONG).show();
}else
new StartLocationAlert(SplashActivity.this);
}
private void getpermission() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissionforApp();
}
}
private void requestPermissionforApp() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(this, "Current Location needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getPackageName(), null));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123);
}
}
private void getDeviceToken() {
final Thread thread = new Thread() {
@Override
public void run() {
Log.e(">>>>>>>>>>>>>>", "thred IS running");
SharedPreferenceWriter mPreference = SharedPreferenceWriter.getInstance(getApplicationContext());
try {
if (mPreference.getString(SharedPreferenceKey.device_token).isEmpty()) {
String token = FirebaseInstanceId.getInstance().getToken();
// String token = android.provider.Settings.Secure.getString(getApplicationContext().getContentResolver(),
// android.provider.Settings.Secure.ANDROID_ID);
Log.e("Generated Device Token", "-->" + token);
if (token == null) {
getDeviceToken();
} else {
mPreference.writeStringValue(SharedPreferenceKey.device_token, token);
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
super.run();
}
};
thread.start();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
try {
if (requestCode == 123) {
int lenght = grantResults.length;
for (int grantlength = 0; grantlength < grantResults.length; grantlength++) {
if (grantResults[grantlength] == PackageManager.PERMISSION_GRANTED) {
getDeviceToken();
SharedPreferenceWriter.getInstance(this).writeBooleanValue(SharedPreferenceKey.permission_granted_location, true);
gpsTracker = new GPSTracker(this, SplashActivity.this);
startActivity(new Intent(SplashActivity.this, Login.class));
finish();
} else if (grantResults[grantlength] == PackageManager.PERMISSION_DENIED) {
boolean b = ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[grantlength]);
if (!b) {
requestPermissionforApp();
} else {
requestPermissionforApp();
}
} else {
requestPermissionforApp();
}
}
} else {
requestPermissionforApp();
}
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK: {
// showDialog();
//new SplashActivity();
//gpsTracker = new GPSTracker(this, SplashActivity.this);
if (SharedPreferenceWriter.getInstance(SplashActivity.this).getBoolean(SharedPreferenceKey.currentLogin,false))
{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}else {
startActivity(new Intent(SplashActivity.this, Login.class));
finish();
}
// All required changes were successfully made
break;
}
case Activity.RESULT_CANCELED: {
new StartLocationAlert(SplashActivity.this);
// The user was asked to change settings, but chose not to
break;
}
default: {
break;
}
}
break;
}
}
}
我的清单文件:
<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.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-feature
android:name="android.hardware.camera"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/app_logo"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@drawable/app_logo"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="android.support.multidex.MultiDexApplication"
>
<activity
android:name="com.sats.quickeats.activities.SplashActivity"
android:theme="@style/TranparentToolbar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.sats.quickeats.activities.SignUp"
android:theme="@style/TranparentToolbar"
android:windowSoftInputMode="stateVisible|adjustResize"/>
<activity
android:name="com.sats.quickeats.activities.Otp"
android:theme="@style/TranparentToolbar"/>
<activity
android:name="com.sats.quickeats.activities.MainActivity"
android:windowSoftInputMode="adjustPan"/>
<activity android:name="com.sats.quickeats.activities.MapDrawerActivity"/>
<activity android:name="com.sats.quickeats.activities.Description"/>
<activity android:name="com.sats.quickeats.activities.FoodMenu"/>
<activity android:name="com.sats.quickeats.activities.MyKart"/>
<activity android:name="com.sats.quickeats.activities.UserAddress"/>
<activity android:name="com.sats.quickeats.activities.PaymentMode"/>
<activity android:name="com.sats.quickeats.activities.FacebookLogin"/>
<activity android:name="com.sats.quickeats.activities.GoogleLogin"/>
<activity android:name="com.sats.quickeats.activities.MapForPayment"/>
<activity android:name="com.sats.quickeats.activities.Login"/>
<activity android:name="com.sats.quickeats.activities.CreditCard"/>
<activity
android:name="com.sats.quickeats.activities.PaymentBackground"
android:theme="@style/TranparentToolbar"
android:windowSoftInputMode="stateVisible|adjustResize"/>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id"/>
<!-- <meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/release_key" />-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/release_key"/>
<activity
android:name="com.sats.quickeats.activities.SearchMap"
android:label="@string/title_activity_search_map"/>
<activity android:name="com.sats.quickeats.activities.Settings"/>
<activity android:name="com.sats.quickeats.activities.WebviewActivity"/>
<activity android:name="com.sats.quickeats.activities.ChangePassword"/>
<activity android:name="com.sats.quickeats.activities.ContactUs"/>
<activity android:name="com.sats.quickeats.activities.Notification"/>
<activity android:name="com.sats.quickeats.activities.MyFav"/>
<activity android:name="com.sats.quickeats.activities.MapForOrderDispatch"/>
<activity android:name="com.sats.quickeats.activities.MapRouteAct"/>
<activity android:name="com.sats.quickeats.activities.TakeImage"/>
<activity
android:name="com.sats.quickeats.util.TakePhoto"
android:hardwareAccelerated="false"/>
<activity
android:name="com.sats.quickeats.activities.ForgotPass"
android:theme="@style/TranparentToolbar"/>
<activity android:name="com.sats.quickeats.activities.SearchFilter"/>
<activity
android:name="com.sats.quickeats.activities.ResetPass"
android:theme="@style/TranparentToolbar"/>
<service android:name="com.sats.quickeats.fcm.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name="com.sats.quickeats.fcm.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="io.fabric.ApiKey"
android:value="44b5915daf7330d0de425de1d6bc82af9e2d242a"/>
<receiver
android:name="com.sats.quickeats.activities.DeliveryBoytStatus"
android:process=":remote"/>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.sats.quickeats.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
<!--<provider-->
<!--android:name="android.support.v4.content.FileProvider"-->
<!--android:authorities="com.mobulous.restaurantapp.fileprovider"-->
<!--android:exported="false"-->
<!--android:grantUriPermissions="true"-->
<!--tools:replace="android:authorities">-->
<!--<meta-data-->
<!--android:name="android.support.FILE_PROVIDER_PATHS"-->
<!--android:resource="@xml/file_paths"-->
<!--tools:replace="android:resource"/>-->
<!--</provider>-->
<!--android:name="com.sats.quickeats.util.RestroApplication"-->
</application>
</manifest>
感谢您的帮助。
答案 0 :(得分:2)
您尚未在设置中启用GPS,
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
当gps未开启时,这将返回false
,并且由于if(statusOfGPS)
,您将停留在启动画面活动中。在设备上打开GPS,你就可以开始了。在manifest
文件中为您提供所需的权限。
您还可以使用manager.isLocationEnabled()
检查当前的位置状态。