Current Location updates in 10 second using Broadcast and Services in Android Example

Current Location Update in 10 Second Using Broadcast and Service in Android

 


MainActivity.java

package example.deepsingh44.com.appfirst;

import android.app.DownloadManager;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.net.Uri;

import android.os.Environment;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    BroadcastReceiver br;


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        br = new BroadcastReceiver() {

            @Override

            public void onReceive(Context context, Intent intent) {

                double lat = intent.getExtras().getDouble("lat");

                double lon = intent.getExtras().getDouble("lon");

                Toast.makeText(context, lat+"\n"+lon, Toast.LENGTH_SHORT).show();

            }

        };


        IntentFilter inf=new IntentFilter();

        inf.addAction("example.deepsingh44.com.appfirst.DEEP");

        registerReceiver(br,inf);

        startService(new Intent(this,MyLocation.class));

    }


    @Override

    protected void onStop() {

        super.onStop();

        unregisterReceiver(br);

    }

}


MyLocation.java

package example.deepsingh44.com.appfirst;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.location.Location;

import android.location.LocationListener;

import android.location.LocationManager;

import android.os.Bundle;

import android.os.IBinder;

import android.support.annotation.Nullable;

import android.widget.Toast;


public class MyLocation extends Service implements LocationListener {

    private LocationManager locationmanager;

    

    @Override

    public void onCreate() {

        super.onCreate();

        locationmanager=(LocationManager)getApplicationContext().

        getSystemService(Context.LOCATION_SERVICE);

//noinspection MissingPermission  locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,0,this);

    }


    @Override

    public void onLocationChanged(Location location) {

        Toast.makeText(this, "Hi"+ location, Toast.LENGTH_SHORT).show();

        double lat=location.getLatitude();

        double lon=location.getLongitude();

        Intent in=new Intent();

        in.putExtra("lat",lat);

        in.putExtra("lon",lon);

        in.setAction("example.deepsingh44.com.appfirst.DEEP");

        sendBroadcast(in);

    }


    @Override

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

    }


    @Override

    public void onProviderEnabled(String s) {

    }


    @Override

    public void onProviderDisabled(String s) {

    }


    @Nullable

    @Override

    public IBinder onBind(Intent intent) {

        return null;

    }

}


AndroidManifest.xml

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="example.deepsingh44.com.appfirst">

    <uses-permission-sdk-23 android:name="android.permission.INTERNET" />

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

    <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        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>

        <service android:name=".MyLocation" />

    </application>

</manifest>

No comments: