Java – how’s it going? Listening location setting is turned on (android app)

So I spent weeks working on my Android application and studying the best way to achieve what I need to do, but I still can't do it correctly Thank you very much for any / all help, because I still have everything

Task:

(assuming that the location / GPS setting is currently turned off), I need to keep my application listening whether the location setting is turned on At this point, only one activity needs to be started

Idea:

These are different ways that I think may be useful:

>Locationlistener using "onproviderenabled" > gpsstatslistener using "ongpstatuschanged" and "gps_event_started" > gpsprovider requires a satellite (to determine whether it starts), Or use "available" constant / int of gpsprovider in some way > settinginjectorservice > settings using "action_service_int" (and / or) "action_rejected_setting_changed" and "ongetenabled" or "isenabled" Secure uses "location_mode"= "Location_mode_off" > a contentobserver / contentresolver > intent getaction (...) > some kind of "if / else"

Question:

Any suggestions or answers to any of the following questions are highly appreciated

>Which of these ideas are the best way to accomplish the task? The simpler the better, but the most important thing is that it needs to listen all the time and respond immediately when the opening position is set. > Which of the above ideas has the best effect, and how will I implement it? (for example, do I need a broadcastlistener? Or a service? How will it be combined?

I really appreciate any advice or help you can give me I still have all this, but I have enough confidence to do it and am eager to release my first application So thank you, it means a lot and will help me a lot

Edit:

OK, that's what I've got so far

Heir my receiver:

MyReceiver. Java

public class MyReceiver extends BroadcastReceiver {

    private final static String TAG = "LocationProviderChanged";

    boolean isGpsEnabled;
    boolean isNetworkEnabled;



    public MyReceiver() {
    // EMPTY

    // MyReceiver Close Bracket
    }



    // START OF onReceive
    @Override
    public void onReceive(Context context,Intent intent) {


        // PRIMARY RECEIVER
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {

            Log.i(TAG,"Location Providers Changed");

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            Toast.makeText(context,"GPS Enabled: " + isGpsEnabled + " Network Location Enabled: " + isNetworkEnabled,Toast.LENGTH_LONG).show();

            // START DIALOG ACTIVITY
            if (isGpsEnabled || isNetworkEnabled) {
                Intent runDialogActivity = new Intent(context,DialogActivity.class);
                context.startActivity(runDialogActivity);

            }

        }



        // BOOT COMPLETED (REPLICA OF PRIMARY RECEIVER CODE FOR WHEN BOOT_COMPLETED)
        if (intent.getAction().matches("android.intent.action.BOOT_COMPLETED")) {

            Log.i(TAG,"Location Providers Changed Boot");

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            Toast.makeText(context,"GPS Enabled Boot: " + isGpsEnabled + " Network Location Enabled Boot: " + isNetworkEnabled,DialogActivity.class);
                context.startActivity(runDialogActivity);

            }

        }



    // onReceive CLOSE BRACKET
    }



// CLOSE OF FULL CLASS
}

And this is what manifest looks like:

Manifest xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ender.projects.receivertest">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".DialogActivity">
        </activity>

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.location.PROVIDERS_CHANGED" />

                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

In addition to these files, I have the following files: mainactivity Java and dialogactivity Java, with layout files, activity_ main. XML and activity_ dialog. XML matches them

Therefore, if I understand correctly, when the user downloads the application and opens it, my mainactivity Java and the corresponding layout will start But I just use it as my preferred screen Therefore, once they open the application for the first time, the broadcast receiver should automatically start listening. Is the location setting on and correct? I also want the broadcast listener to keep listening even after it receives the initial broadcast (if they turn off the location setting, my onReceive will still trigger, and then turn them on again..)

So (a) how does my code look so far? (B) What needs to be added to complete what I just described? And (c) when I set location to on, running it will throw this error What should I do java. Lang.runtimeexception: unable to start receiver com bryce. projects. servicesthreadsetc. MyReceiver:android. util. Androidruntimeexception: calling startactivity() from outside the activity context requires flag_ ACTIVITY_ NEW_ Task flag Is this really what you want?

Thank you again for your help!

Solution

Since you don't need to actually get the location, the best implementation you need is broadcastreceiver

This is the best choice because you don't need to run the service all the time (resulting in additional battery consumption), and you will be able to start your activities from broadcastreceiver

Using intent filter and broadcastreceiver, the operating system will start your application as long as the location setting has been changed (enabled or disabled). If enabled, you can start activities from broadcastreceiver

First, add an intent filter, which will be captured when the operating system issues an implicit intent with changed settings:

<receiver
    android:name=".LocationProviderChangedReceiver"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

Then, in locationproviderchangedreceiver In Java, your implementation will be as follows:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.util.Log;
import android.widget.Toast;

public class LocationProviderChangedReceiver extends BroadcastReceiver {
    private final static String TAG = "LocationProviderChanged";

    boolean isGpsEnabled;
    boolean isNetworkEnabled;

    @Override
    public void onReceive(Context context,Intent intent) {
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
        {
            Log.i(TAG,"Location Providers changed");

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            //Start your Activity if location was enabled:
            if (isGpsEnabled || isNetworkEnabled) {
                  Intent i = new Intent(context,YourActivity.class);
                  context.startActivity(i);
            }
        }
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>