Search Unity

ANDROID PLUGIN HELP: Lack of Context for use registerReceiver

Discussion in 'Android' started by YJack, Feb 13, 2019.

  1. YJack

    YJack

    Joined:
    Apr 7, 2010
    Posts:
    44
    Hello guys, I'm pretty new on Android/Java plugin for Unity so I'm probably with some very silly question. Sorry about that.

    My object is to get battery temperature with the code below. The question is on the comments.

    Code (CSharp):
    1. package com.yjack.unity;
    2. import android.util.Log;
    3. import android.content.BroadcastReceiver;
    4. import android.content.Context;
    5. import android.content.Intent;
    6. import android.content.IntentFilter;
    7. import android.os.BatteryManager;
    8.  
    9. public class MyPlugin
    10. {
    11.     private static final MyPlugin ourInstance = new MyPlugin();
    12.     private static final String LOGTAG = "YJack";
    13.     public static MyPlugin getInstance() {
    14.         return ourInstance;
    15.     }
    16.     private int temperature;
    17.  
    18.     private MyPlugin()
    19.     {
    20.         Log.i(LOGTAG, "Created MyPlugin");
    21.         BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver()
    22.         {
    23.             @Override
    24.             public void onReceive(Context context, Intent intent)
    25.             {
    26.                 updateTemperature(intent);
    27.             }
    28.         };
    29.         loadBatterySection(batteryInfoReceiver);
    30.     }
    31.  
    32.     public int getTemperature()
    33.     {
    34.         return temperature;
    35.     }
    36.  
    37.     private void loadBatterySection(BroadcastReceiver batteryInfoReceiver)
    38.     {
    39.         IntentFilter intentFilter = new IntentFilter();
    40.         intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
    41.         registerReceiver(batteryInfoReceiver, intentFilter); //registerReceiver is a method that I dont have at this class, probably because this class is not a Context. How I solve that?
    42.     }
    43.  
    44.     private void updateTemperature (Intent intent)
    45.     {
    46.         temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
    47.     }
    48. }
     
  2. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,966
    You can get the "context" in this context (;)) by using Unity's activity.
    As activity extends from Context, you can use UnityPlayer.currentActivity variable to register the receiver.

    Code (JavaScript):
    1. public static Context getCurrentContext()
    2. {
    3.         Context context = null;
    4.         try
    5.         {
    6.             context = UnityPlayer.currentActivity;
    7.         }
    8.         catch (Throwable e)
    9.         {
    10.             Debug.log("Info", "Running on non-unity platform");
    11.         }
    12.  
    13.         return context;
    14. }
    15.  
    It's always good to not access currentActivity like above as you can reuse the same code other than in Unity too. To achieve this, always pass the context from c# and let your class receive the context in constructor.
     
    Udhayarajan0504, sbson and YJack like this.
  3. q3mobility

    q3mobility

    Joined:
    Apr 1, 2019
    Posts:
    1

    Where we need to write this code. In unity c# script file?????
     
  4. Voxel-Busters

    Voxel-Busters

    Joined:
    Feb 25, 2015
    Posts:
    1,966
    No. This is on Native (Java)