wait for a function to run and then start activity

后端 未结 1 1111
-上瘾入骨i
-上瘾入骨i 2020-12-22 05:13

When the users presses the start button , a check runs to see if GPS is enabled and then starts an activity.The checkGPS uses alertmanager.

case R.id.startbt         


        
相关标签:
1条回答
  • 2020-12-22 06:07

    1) in onCreate() method, register LocationListener on your Activity :

       // get location manager
       LocationManager lManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       Button btnStart = (Button)findViewById(R.id.btnStart);
       btnStart.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
                lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, YourActivity.this);
           }
       });
    

    2) your activity should implement LocationListener:

       public class YourActivity extends Activity implements LocationListener {
    

    3) override methods of LocationListener :

    @Override public void onLocationChanged(Location location) { // when the position has changed Log.d(TAG, "location changed.");
    }

    @Override
    public void onProviderDisabled(String provider) {
         // when the source (GSP or GSM network) are disabled
         Log.d(TAG, "the source "+provider+" has been disabled");
    }
    
    @Override
    public void onProviderEnabled(String provider) {
         Log.i(TAG, "the source "+provider+" has been enabled");
         //here you should test if the provider enabled is GPS , if yes , then launch your GPSActivity
         if(provider.equals("gps") {
               Intent gpslocation=new Intent(this,selection.class);
               startActivity(gpslocation);
         }
         else {
               //launch your alert dialog here
         }
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
         Log.i(TAG, "source status "+provider+" has been changed to : "+status);
    }
    
    0 讨论(0)
提交回复
热议问题