Skip to content

Instantly share code, notes, and snippets.

@talhahasanzia
Last active July 8, 2018 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save talhahasanzia/c2edde310ab8ad4e1fd222c9aee98680 to your computer and use it in GitHub Desktop.
Save talhahasanzia/c2edde310ab8ad4e1fd222c9aee98680 to your computer and use it in GitHub Desktop.
Sending info from a receiver in a service class to main activity class
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testbed.projects.stackoverflow">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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=".BackgroundService">
</service>
</application>
</manifest>
package com.testbed.projects.stackoverflow;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
/**
* The service will Run in background
* mTickReceiver will call onReceive every minute
* onReceive will send a broadcast intent with data
* that broadcast intent will be received by MainActivity
* hence data from receiver (in a service) to mainactivity is sent successfully
*/
public class BackgroundService extends Service {
private static BroadcastReceiver mTickReceiver;
public BackgroundService()
{
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onCreate()
{
registerReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(mTickReceiver);
mTickReceiver = null;
}
private void registerReceiver()
{
// THIS IS RECEIVER
mTickReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent) // this will fire every minute
{
// sample data
double SampleLatitude=52.01566;
double SampleLongitude=65.00487;
// assuming above coordinates are from some location manager code
Intent i=new Intent();
i.setAction("LocationData");
i.putExtra("Latitude",SampleLatitude);
i.putExtra("Longitude",SampleLongitude);
// PREPARE BROADCAST FOR MAINACTIVITY
sendBroadcast(i); // this broadcast will be received by mainactivity
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK); // this will broadcast Intent every minute
registerReceiver(mTickReceiver, filter);
}
}
package com.testbed.projects.stackoverflow;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* since MyBroadcast receiver is inner class, any data it receives can be saved in MainActivity's "final" declared variables.
* hence we receive data in MainActivity from a service/receiver
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i=new Intent(this, BackgroundService.class);
startService(i);
IntentFilter filter = new IntentFilter("LocationData");
registerReceiver(new MyBroadcastReceiver(),filter);
}
class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// GET BROADCAST FROM RECEIVER IN THE BACKGROUND SERVICE CLASS
if (intent.getAction() == "LocationData")
{
double lat=intent.getDoubleExtra("Latitude",0);
double lng=intent.getDoubleExtra("Longitude",1);
String LocationDataFromService=lat+","+lng;
Log.d("LocationDataFromService",LocationDataFromService); // this works, it is checked
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment