繁体   English   中英

活动不可见时调用的Android onResume方法

[英]Android onResume method called when activity is not visible

我想在我的应用程序中访问gps服务。因此我使用了Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 在按钮的onClick()方法中。调用Intent后,我启动服务以侦听位置更新。在onResume方法中,我检查gps服务。如果已禁用,则停止服务。但是onResume方法被调用当显示“位置设置”并且服务最终在用户可以打开gps之前停止时。

码:

     Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(i);
     gps_intent=new Intent(AddOffers.this, StatsGpsService.class);
     gps_intent.putExtra("offr_act_ind",true);
     startService(gps_intent);



 @Override
     protected void onResume() {
     super.onResume();
     LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
     boolean isproviderenabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if(isproviderenabled==false){
        if(gps_intent!=null){
            Log.e("g","g");
        stopService(gps_intent);
    }}

StatsGpsService.java:

public class StatsGpsService extends Service {
    LocationListener locationListener;
    LocationManager locationManager;
    boolean flag1;

    public StatsGpsService() {
        super();

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("Service","Created");


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        handleLocation(intent);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("Service","Destroyed");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void handleLocation(Intent intent){
        if(intent!= null) {
            flag1 = intent.getBooleanExtra("offr_act_ind", false);

            locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    if (flag1 == true) {

                        Intent intent = new Intent("loc_updts");
                        intent.putExtra("stat_lat", location.getLatitude());
                        intent.putExtra("stat_longt", location.getLongitude());
                        sendBroadcast(intent);
                    }
                }

                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {

                }

                @Override
                public void onProviderEnabled(String s) {

                }

                @Override
                public void onProviderDisabled(String s) {
                    Log.e("Provider","Disabled");

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


            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }

            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);
        }
    }
}

不要在onResume上检查GPS状态。 使用startActivityForResult打开设置。

并检查onActivityResult上的Gps状态

要打开设置,请使用以下代码

Intent SettingIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
SettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(SettingIntent,Utils.SETTING_INTENT_REQUESTCODE// any int code you can use);

要处理结果,您需要重写Activity的onActivityResult方法

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Utils.SETTING_INTENT_REQUESTCODE// this should be same int that you passed to start acitivity) {
            LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
            boolean isproviderenabled=locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if(isproviderenabled==false){
                if(gps_intent!=null){
                    Log.e("g","g");
                    stopService(gps_intent);
                }
            }
        }
    }

希望它对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM