Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

android, how to request location inside a timer on a service with the screen off

I want to send latitude and longitude over SMS while the screen is off. For now, with a service and a timer I can send SMS every 5 seconds with the screen off, but I can't figure out how to add the current location to the SMS. What is the solution for getting location while in screen off?

I have tried several approaches like this two, but I can never send the location via sms with the screen off.

Get GPS location via a service in Android

Android, get the location when the screen is off

Here is how I have set the timer in the service

public class MyService extends Service {

Timer timer1;

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

    timer();
    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    //TODO for communication
    return null;
}

//-Main task handler-----------------------------------------
private void timer() {
    Timer t = new Timer();
    //Set the schedule function and rate
    t.scheduleAtFixedRate(new TimerTask() {
                              @Override
                              public void run() {
                                  Log.i("MyServiceJ", "init task");

                                  //get latitude and longitude

                                  sendSMS( "lat and long", "123456");

                                  Log.i("MyServiceJ", "finish task");
                              }
                          },
            0,
            5000);
}

And here is my MainActivity

public class MainActivity extends AppCompatActivity {

public final static String MESSAGE_KEY ="com.example.message_key";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, PackageManager.PERMISSION_GRANTED);
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, PackageManager.PERMISSION_GRANTED);
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.RECEIVE_SMS}, PackageManager.PERMISSION_GRANTED);
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PackageManager.PERMISSION_GRANTED);
}

public void startBackGroundProcessButtonClick(View view) {

    Intent i= new Intent(this, MyService.class);
    this.startService(i);
}
question from:https://stackoverflow.com/questions/65939885/android-how-to-request-location-inside-a-timer-on-a-service-with-the-screen-off

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...