Data Binding from Android project to ViewModel - android

I am trying to get list of available wifi network and display it in a list view. In PCL project, i created an interface 'IWifiService'.
public interface IWifiService
{
void GetWifiList();
}
In android project a class WifiService to get all networks list.
[assembly:Dependency(typeof(WifiService))]
namespace WifiConnectivity.Droid.WifiService
{
public class WifiService : IWifiService
{
private static WifiManager wifi;
private WifiReceiver wifiReceiver;
public static List<string> WiFiNetworks;
public void GetWifiList()
{
WiFiNetworks = new List<string>();
var context = Android.App.Application.Context;
wifi = (WifiManager)context.GetSystemService(Context.WifiService);
wifiReceiver = new WifiReceiver();
context.RegisterReceiver(wifiReceiver, new IntentFilter(WifiManager.ScanResultsAvailableAction));
wifi.StartScan();
}
public class WifiReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
IList<ScanResult> scanWifiNetworks = wifi.ScanResults;
foreach(ScanResult wifinetwork in scanWifiNetworks)
{
WiFiNetworks.Add(wifinetwork.Ssid);
}
}
}
}
}
And from ViewModel i am calling a Dependency Service like this:
var service = DependencyService.Get<IWifiService>();
service.GetWifiList();
Now the class WifiReceiver cannot return anything, so i am not being able to get those list of networks in my ViewModel. How can i get this list into my ViewModel?

I would suggest that you return a List in your GetWifiList method so the interface looks something like this:
public interface IWifiService
{
void GetWifiList(Action<List<string>> action);
}
And then your GetWifiList method will look like this:
public static Action<List<string>> CallBackAction;
public void GetWifiList(Action<List<string>> callBackMethod)
{
WiFiNetworks = new List<string>();
var context = Android.App.Application.Context;
wifi = (WifiManager)context.GetSystemService(Context.WifiService);
wifiReceiver = new WifiReceiver();
context.RegisterReceiver(wifiReceiver, new IntentFilter(WifiManager.ScanResultsAvailableAction));
wifi.StartScan();
CallBackAction= callBackMethod;
}
And then in your OnRecieve method, you execute the callback method:
public override void OnReceive(Context context, Intent intent)
{
IList<ScanResult> scanWifiNetworks = wifi.ScanResults;
foreach(ScanResult wifinetwork in scanWifiNetworks)
{
WiFiNetworks.Add(wifinetwork.Ssid);
}
CallBackAction(WiFiNetworks);
}
And now the most important part of the callback method and what to pass as your argument:
A System.Action is basically a method reference where you pass the name of your call back method something like below:
var service = DependencyService.Get<IWifiService>();
service.GetWifiList(MyCallBackAction);
Where MyCallBackAction is as below:
public void MyCallBackAction(List<string> wifiNetworks)
{
// this method will be called every time you get a callback at OnRecieve
}

Related

LifeCycleOwner in Broadcast reciever

I am using Android Room to store my application data. After device reboot I need to retrieve them and do some functions. I am using LiveData for getting data from database. But I cannot bind Broadcast Reciever as owner. How should I resolve this problem?
public class BootReciever extends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
if (context != null) {
ProductUIRepository mRepository = new ProductUIRepository(context.getApplicationContext());
mRepository.findAllProducts().observe(this, new android.arch.lifecycle.Observer<List<ProductUI>>() {
#Override
public void onChanged(#Nullable List<ProductUI> productUIS) {
NotificationMan.setAlarmForProducts(context, productUIS);
}
});
}
}
}
using "this" makes that applications isn't compiling.

leakcanary stacktrace hard to understand

I have the below stack trace from leak canary with which I am not sure how my Activity is getting leaked
static LGCobtextHelper.mLGContext
references LGContext.mContext
references
ResourcesContextWrapperFactory$WebViewContextWrapper.mBase
references
com.*.*.activity.MyActivity.networkMonitor
references
com.*.*.NetworkMonitor.mPendingResult
references
android.app.LoadedApk$ReceiverDispatcher$Args.this$0
references
LoadedAok$ReceiverDispathcer.mContext
leaks MyActivity instance
MyActivity extends BaseActivity, which registers onResume() and unregisters onPause(), so not sure which leaks the activity
NetworkMonitor.java
public class NetworkMonitor extends BroadcastReceiver {
private final WebSocketClient webSocketClient;
private final ArmingHelper armingHelper;
private final ShutdownManager shutdownManager;
private final CameraThumbnailCache cameraThumbnailCache;
private final CameraAccessManager cameraAccessManager;
private final JoustLogger joustLogger;
private Activity registeredActivity;
private String currentNetworkName;
private List<NetworkStatusChangeListener> networkChangeListeners;
public interface NetworkStatusChangeListener {
void onNetworkUp();
void onNetworkDown();
}
public NetworkMonitor(WebSocketClient webSocketClient, ArmingHelper armingHelper, ShutdownManager shutdownManager, CameraThumbnailCache cameraThumbnailCache, CameraAccessManager cameraAccessManager, JoustLogger joustLogger) {
this.webSocketClient = webSocketClient;
this.armingHelper = armingHelper;
this.shutdownManager = shutdownManager;
this.cameraThumbnailCache = cameraThumbnailCache;
this.cameraAccessManager = cameraAccessManager;
this.joustLogger = joustLogger;
networkChangeListeners = new ArrayList<>();
}
// Activities *must* call this method in onResume() in order for
// the app to watch for network changes
public void startListeningForNetworkChanges(Activity registeringActivity) {
if (!(registeringActivity instanceof NetworkStatusChangeListener)) {
throw new IllegalArgumentException("Registering Activity must implement NetworkStatusChangeListener");
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intentFilter.addAction(GlobalConstants.ANDROID_NET_WIFI_WIFI_STATE_CHANGED);
registeringActivity.registerReceiver(this, intentFilter);
this.registeredActivity = registeringActivity;
registerListenerForNetworkChanges((NetworkStatusChangeListener)registeringActivity);
}
// Activities *must* call this method in onPause() in order to properly
// unregister the receiver that was set in onResume()
public void stopListeningForNetworkChanges(Activity registeringActivity) {
registeringActivity.unregisterReceiver(this);
unregisterListenerForNetworkChanges((NetworkStatusChangeListener)registeringActivity);
registeredActivity = null;
}
// Fragments can use this method to register for Network change updates, call in onResume()
public void registerListenerForNetworkChanges(NetworkStatusChangeListener listener) {
networkChangeListeners.add(listener);
}
// Fragments need to unregister in onPause()
public void unregisterListenerForNetworkChanges(NetworkStatusChangeListener listener) {
networkChangeListeners.remove(listener);
}
#Override
public void onReceive(Context context, Intent intent) {
checkNetworkConnection();
}
public void checkNetworkConnection() {
if (registeredActivity != null) {
final ConnectivityManager connectivityManager = (ConnectivityManager) registeredActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
String newNetworkName = networkInfo.getTypeName();
if (currentNetworkName == null || !currentNetworkName.equals(newNetworkName)) {
Timber.d("Network(%s) Connected", newNetworkName);
// Our network was down, but now it's up. Validate the Websocket
currentNetworkName = newNetworkName;
cameraThumbnailCache.clearInternalURLPreferences();
webSocketClient.reopenWebsocketIfPossible();
cameraAccessManager.onNetworkUp();
if (ActivityBehaviorHelper.needsSecurityCountdown(registeredActivity)) {
armingHelper.startTimerIfReady();
}
for (NetworkStatusChangeListener listener : networkChangeListeners) {
listener.onNetworkUp();
}
joustLogger.onNetworkUp();
}
} else {
Timber.w("Network Down");
currentNetworkName = null;
cameraAccessManager.onNetworkDown();
joustLogger.onNetworkDown();
shutdownManager.onNetworkDown();
for (NetworkStatusChangeListener listener : networkChangeListeners) {
listener.onNetworkDown();
}
}
}
}
}
BaseActivity.java
#Override
protected void onResume() {
super.onResume();
networkMonitor.startListeningForNetworkChanges(this);
}
#Override
protected void onPause() {
networkMonitor.stopListeningForNetworkChanges(this);
super.onPause();
}
It looks like you probably don't need to be holding a reference to the Activity in that NetworkMonitor class. That's probably the source of your memory leak - the Activity reference is likely being held after the Activity is destroyed. Looks like you could just pass the context in as a parameter to the methods that need it.
Also, For a few of the spots where Activity context is being used here, like context.getSystemService(Context.CONNECTIVITY_SERVICE), you could use Application context instead and possibly avoid needing an Activity reference altogether.

Android :get an object from an inner class

I defined a broadcast receiver as an inner class .And i wan't to get an object from the receiver to the external class ,jut like this :
class External {
public object ;
class Inner {
object = something ;
getObject () { return object; }
}//end Inner class
method (){
Inner inn = new Inner();
inn.getObject(); //here i would like to get the object from inner class
}
}
But it doesent work for me , i'm gettin an empty object ,this is my real code :
public class Wifi extends Activity implements OnClickListener{
/** Called when the activity is first created. */
WifiManager wifi;
Button enab;
String resultsString ;
String[] myStringArray;
public class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)){
List<ScanResult> results = wifi.getScanResults();
resultsString = results.toString() ;
//Log.i("resultsString",resultsString);
List<WifiConfiguration> resultsConf = wifi.getConfiguredNetworks();
}
}
/***** I would like to get resultsString *******/
public String getResult(){
return resultsString ;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi);
ConnectivityManager cxMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
Receiver receiver = new Receiver();
/***** I would like to get resultsString in this method but I'm getting a null **/
registerReceiver(receiver,new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
registerReceiver(receiver,new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
Log.i("receiver.getResult()",receiver.getResult()+"");
}
}
And this is my logCat
03-27 12:28:51.947: I/receiver.getResult()(24524): null
Thank you .I home someone will help me :)
Try this..
I guess your getting error in below line
Log.i("receiver.getResult()",receiver.getResult());
may be receiver is null so that you can check it like below add ""+
Log.i("receiver.getResult()",""+receiver.getResult());
EDIT:
IntentFilter intentFilter =new IntentFilter(Receiver.ACTION_TEXT_CAPITALIZED);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
/* create new broadcast receiver*/
Receiver receiver = new Receiver();
registerReceiver(receiver, intentFilter);
Add this in your Receiver class
public final static String ACTION_TEXT_CAPITALIZED= "com.android.guide.exampleintentservice.intent.action.ACTION_TEXT_CAPITALIZED";
You can send information like below
Intent resultBroadCastIntent =new Intent();
resultBroadCastIntent.setAction(Receiver.ACTION_TEXT_CAPITALIZED);
resultBroadCastIntent.setAction(ConnectivityManager.CONNECTIVITY_ACTION);
resultBroadCastIntent.setAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
resultBroadCastIntent.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(resultBroadCastIntent);

The right way to dynamically control location tracking (registering / unregistering location broadcast receiver)

I want to dynamically control location tracking (registering / unregistering location broadcast receiver). This is how I am planning to do it. I have two questions :
What are the mistakes in the implementation below because all this concept is still very theoretical to me as I am very new to android/java dev. Still building concepts!
How do I pass some EXTRA_INFO from my location library class to the location receiver.
IMPLEMENTATION:
I have a library class LocationLibrary.java which consists of two methods. They do as the name suggest. The location tracking should start when I call startTracking(). Plz note the extraInfo that needs to be passed to myLocationReceiver. The tracking should stop when stopTracking() is called.
Code snippet:
public class LocationLibraray
{
private static BroadcastReceiver myLocationReceiver;
public LocationLibraray(Context context)
{
this.ctx = context;
myLocationReceiver = new MyLocationReceiver();
}
public void startTracking(Context context, String extraInfo)
{
IntentFilter filter = new IntentFilter();
filter.addAction("com.app.android.tracker.LOCATION_READY");
context.registerReceiver(myLocationReceiver, filter);
// NEED TO PASS extraInfo to myLocationReceiver for some processing, but HOW?
}
public void stopTracking(Context context)
{
context.unregisterReceiver(locationReceiver);
}
}
MyLocationReceiver.java
public class MyLocationReceiver extends BroadcastReceiver {
public void onReceive(final Context context, Intent intent) {
if ((intent.getAction() != null) &&
(intent.getAction().equals("com.app.android.tracker.LOCATION_READY")))
{
//GET THAT EXTRA INFO FROM LocationLibrary class and process it here
}
}
}
Please help me out. Thnx!
Why not add a constructor to MyLocationReceiver?
public class MyLocationReceiver extends BroadcastReceiver {
String info = "";
public MyLocationReceiver(String extraInfo)
{
this.info = extraInfo;
}
........
public void onReceive(final Context context, Intent intent) {
if ((intent.getAction() != null) &&
(intent.getAction().equals("com.app.android.tracker.LOCATION_READY")))
{
if (info.contains("Hi"))
//do some stuff
}
}
}
And you would instantiate it like this:
myLocationReceiver = new MyLocationReceiver(new String("Hello!"));

android application class in thread

i need to use my application class inside my thread which is started with InterService.
in my IntentService i have the following code:
protected void onHandleIntent(Intent intent) {
final ResultReceiver receiver = intent.getParcelableExtra("receiver");
context = getBaseContext();
app = (AppLoader)getApplicationContext();
ConnectionThread thread = new ConnectionThread(receiver, context, app.getNewApp());
this is my Thread:
public ConnectionThread (ResultReceiver receiver, Context context, AppLoader app)
{
this.receiver = receiver;
this.context = context;
this.activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
this.app = app;
}
#Override
public void run() {
Log.d("ConnectionThread","Starting Server Connection");
try {
while(isThereActivityRunning()) {
if(app.isInternetOn())
{
Log.d("ConnectionThread", app.getText());
results = sendGetMessage();
b.putString("results", results);
receiver.send(2, b);
}
this is my application:
public class AppLoader extends Application{
private AppLoader newApp;
public void onCreate()
{
super.onCreate();
}
public AppLoader getNewApp()
{
if(newApp == null)
newApp = new AppLoader();
return newApp;
}
i get a java.lang.NullPointerException and i can't figure out why..
You can't create your own Application instance, i.e.
newApp = new AppLoader();
is not meant to be called. Android creates the app for you, or at least it does if you declared your application class in the manifest, i.e.
<application ... android:label="#string/app_name" android:name="AppLoader" android:debuggable="true">
It will compile but you won't have access to anything that an Android-instantiated application normally would.
Assuming you have the manifest as above, you already have access to the application instance by calling:
app = (AppLoader)getApplicationContext();
so use that and delete the getNewApp() method.

Categories

Resources