In my MainActivity i can take Location with code like this:
private BroadcastReceiver broadcastReceiver;
private Location currentLocation;
#Override
protected void onResume(){
super.onResume();
if(broadcastReceiver == null){
broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
currentLocation = (Location)intent.getExtras().get("coordinates");
}
};
}
registerReceiver(broadcastReceiver, new IntentFilter("location_update"));
}
Now i have WebView in this Activity and want to send Location to JavaScript inside WebActivity.
To send something from Java code to JS code inside WebActivity i crate webinterface class and use code like:
#JavascriptInterface
public String GetHi(){
return "Hi";
}
But how can i send Location?
You can always use the good old Json to pass the data to the Javascript side (The Dark Side henceforth). Serialize your data and pass it as a serialized JSON string to your Dark Side side and then in the Dark Side you can deserialize it into your Javascript Location object.
Related
I´m pretty new in android. I have made communication between two Apps with BroadcastReceiver and intentServices .
The thing is, I want to send information to the app2 from app1. In app1 I need to access a variable which is in MainActivity.class , I need to send it to servicev.class (the service where the intent is handled) but the variable "res" is null when I access it, why does that happen? (App2 calls app1 onHandleIntent and it breaks in res.getOtp() ) I try to create an extra setter getter class and also an intent but getIntent() does not work inside onHandleIntent... how can I achieve to pass res.getOTP (string) ? I really dont want to use SQLite
servicev:
public class servicev extends IntentService {
private static final int RESULT_OK = 1;
protected ResultReceiver mReceiver;
public servicev() {
super("yeah");
}
#Override
protected void onHandleIntent(Intent intent) {
//I receive here the intent from app2 and I need to response with res.getOTP()
helper h = new helper();
String val = intent.getStringExtra("foo");
Intent in = new Intent("com.banorte.bem.movil.veriToken.SendBroadcast");
in.putExtra("resultCode", this.RESULT_OK);
in.putExtra("resultValue", "My Result Value. Passed in: " + h.getRes().getOtp()); //h here is null... setter and getter approach does not work... maybe sqlite could work but it is necesary?
sendBroadcast(in);
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
VTTokenAPI api;
TextView textView;
Button button;
EditText input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidSetup.getInstance().init(this);
helper h = new helper();
api = new VTTokenAPI("FFFFFF");
res = api.getStatus();
res.getOtp(); //correct value
h.setRes(res);
setContentView(R.layout.activity_main);
}
}
helper:
public class helper {
public VTResult getRes() {
return res;
}
public void setRes(VTResult res) {
this.res = res;
}
VTResult res;
}
You are trying to instantiate a new MainActivity which is not the same as the running activity but a new instance.
If you need your IntentService to be able to get data from a running Activity you have options such as using SharedPreferences or SQLite. Instead of keeping the data in memory try to persist it in some database in the onCreate and then try to read it from the storage during handleIntent
I have an Android app that contains a users list with an Avatar for each user. The avatar image file is stored as a local .png file in the apps cache folder. From time to time, another service updates the avatar png files with more current ones (but in no regular order), and I would like to have my list of avatar ImageView update with the new .png files as they are saved to disk.
I have tried subclassing ImageView and adding a FileObserver property to it, however, this isn't seeming to be the most effective.
Does anyone have any recommendations on how to "live" bind a .png to an ImageView so that it will updates if/when the image file on disk changes?
I'm not sure if I should be looking into DataBinding or not because this seems overkill to me.
You can register BroadcastReceiver at activity and receive notifications from your service.
Firstly, you need to create an implementation of BroadcastReceiver like this:
public class YourBroadcastReceiver extends BroadcastReceiver {
/**
* Listener interface for received broadcast messages.
*/
public interface YourBroadcastListener {
void receivedBroadcast();
}
/**
* The Intent action that this Receiver should filter for.
*/
public static final String ACTION = "com.your.package.IMAGES_UPDATED";
private final YourBroadcastListener mListener;
public YourBroadcastReceiver(YourBroadcastListener listener) {
mListener = listener;
}
#Override
public void onReceive(Context context, Intent intent) {
if (mListener != null)
mListener.receivedBroadcast();
}
}
Next, you need to register YourBroadcastReceiver in your activity:
public class MainActivity extends Activity implements YourBroadcastListener {
private YourBroadcastReceiver mReceiver;
#Override
protected void onResume() {
super.onResume();
if(mReceiver == null){
mReceiver = new YourBroadcastReceiver(this);
registerReceiver(mReceiver, new IntentFilter(YourBroadcastReceiver.ACTION));
}
}
#Override
protected void onPause() {
super.onPause();
if(mReceiver != null){
unregisterReceiver(mReceiver);
mReceiver = null;
}
}
#Override
public void receivedBroadcast() {
// Received a broadcast notification that the images has changed - reload it
}
}
And the last, your service need to send brodcast notifications to your activity:
Intent i = new Intent("com.your.package.IMAGES_UPDATED");
sendBroadcast(i);
Take a note: your action string need to be unique to avoid collisions with another applications.
I'm uploading a large image that is converted to Base64 String in a IntentService. I have splitted the String, to not send all the parts on one time.
In my IntentService so, I'm uploading that parts to the Server.
This happens in the background. If the user wants to know how far the uploading process is, I want to add an Item in the Navigation Drawer to start an Activity that shows the progress.
Is it anyway possible to show this progress?
I would be really thankful if someone can tell me how to do this.
I've read somewhat about BroadcastReceiver. But I don't understand how that works and I really don't know if it will matter my concern.
Thanks for any help!
Kind Regards!
Ofcourse you can use broadcast receivers for this. There is another approach. You can use ResultReceiver class to achieve this. Create a CustomResultReceiver class
public class CustomResultReceiver extends ResultReceiver {
private Receiver receiver;
public CustomResultReceiver(Handler handler) {
super(handler);
// TODO Auto-generated constructor stub
}
public interface Receiver {
void onReceiveResult(int resultCode, Bundle resultData);
}
public void setReceiver(Receiver receiver) {
this.receiver = receiver;
}
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
if (receiver != null) {
receiver.onReceiveResult(resultCode, resultData);
}
}
}
In your activity, implement the receiver
public class SomeActivity extends Activity implements CustomResultReceiver.Receiver {
public CustomResultReceiver mReceiver;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReceiver = new CustomResultReceiver(new Handler());
mReceiver.setReceiver(this);
//pass the receiver to the service
intent.putExtra("SOME_TAG", mReceiver);
}
#Override
public void onReceiveResult(int resultCode, Bundle resultData) {
// here you can get the values from the bundle and display the progress accordingly
}
}
Now in your service, whenever you want to send any data to the activity, you can simple do it by
#Override
protected void onHandleIntent(Intent intent) {
ResultReceiver receiver = intent.getParcelableExtra("SOME_TAG");
Bundle b= new Bundle();
//put your progress value in the bundle
b.putString("SOME_DATA", dataValue);
receiver.send(RESULT_CODE, b);
}
Well I would suggest you to upload you image using AsyncTask. Here you will get the benefit of onProgressUpdate() method. What you need to do is:
Create an interface, having a method, say onUploadingUpdate.
Implement that interface in your activity.
Create your AsyncTask class for uploading your image file.
Call this AsyncTask in your activity using its constructor.
In AsyncTask's onProgressUpdate() method call the interface's method. This will give you the progress in activity.
In Activity use this progress to update progress in your navigation drawer.
If you don't understood, post your code. I'll do it for you.
I'm writing a GCM application.
I'm unable to set a received message to a text view.
Check the below code:
public class GcmMessageHandler extends IntentService {
String mes;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
#Override
public void onCreate() {
super.onCreate();
handler = new Handler();
}
#Override
public void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
showMsg();
Log.i("GCM", "Received : (" +messageType+") "+extras.getString("title"));
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
public void showMsg(){
handler.post(new Runnable() {
public void run() {
TextView textview = (TextView)findViewById(R.id.getMsg);
textview.setText(mes );
}
});
}
}
(Error msg : The method findViewById(int) is undefined for the type new Runnable(){})
What you're trying to do is to get the reference of your TextView R.id.getMsg in the layout. You're using the findViewByIdmethod in an IntentService Class which does not provide this method. See Android Documentation
Normally you do that in an Activity or Fragment in lifecycle methods like onCreate or onCreateView etc. where the View is given as parameter value. That looks similar to the following line of code:
TextView textview = (TextView)view.findViewById(R.id.getMsg);
After you've retrieved your TextView reference, you're able to use it in implemented methods.
You need a way to handle data from your Intent Service back to your Application/Activity etc. A possible solution could be to pass your retrieved message data from the IntentService back to the Activity you could use a BroadcastReceiver (as shown here)
Your textview is in service class and textview must be at activity
class . If you want to set data in activity than create an interface
or make textview public static.
I'm Trying to set a value(global) in a function and access it outside function that is in "On create()" in android
I've tried making the global variable static, and I even tried to write it in a "edit text" and parsing it in "on create()" . but it keeps initializing to 0.0 (the variable is a double type)
when i tried to access in "on create()",
oh and i can't return the value because the function is too nested so all hierarchy is too complex. :(
Can anyone help me with this;
public class TryActivity extends Activity
{
double BAT;\\ global value
public void onCreate(Bundle savedInstanceState)
{
disp(); // calling the function disp to set the value to BAT
String To_string=Double.toString(BAT);
System.out.println("Current Battery level ==="+To_string); \\ prints 0.0 the wrong value
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void disp(){
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver(){
public void onReceive(Context context, Intent intent){
double level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
BAT=level;
Textview1 = (EditText) findViewById(R.id.Textview1);
Textview1.setText(Double.toString(BAT)); // sets the correct value
System.out.println("bbbattererrerey 1 "+Double.toString(BAT)); //prints the correct value
}
};
}
Simply initialize the variable as public static gobally in the class. You will be able to access it from anywhere.
Define as public static your variables:
public class TryActivity extends Activity
{
public static double BAT; //global value.
public void onCreate(Bundle savedInstanceState) {
...
...
...
You are getting BAT with value of 0.0 because when your activity starts execute the method onCreate() and the the method disp() that only register the Intent to get the Battery Level.
If you want to get the battery level at the start of your activity you can do it with a function to get the battery level without receiving updates.
public float getMyBatteryLevel() {
Intent batteryIntent = this.getApplicationContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
return batteryIntent.getIntExtra("level", -1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
//* Add this method.
getMyBatteryLevel()
disp(); // calling the function disp to set the value to BAT
String To_string = Double.toString(BAT);
System.out.println("Current Battery level ==="+To_string); //prints the right battery level.
The problem is you don't understand the concept of concurrency. The BroadcastReceiver's onReceive() won't be called immediately. Thus, you are just setting up the BroadcastReceiver in disp(), and not directly touching BAT. BAT will only be filled with the correct value when onReceive() is called.
If you will look in to your logs the System.out.println() you have written in onCreate will be called before the System.out.println() written in onReceive of your BroadcastReceiver even it is written after your disp() method.
Reason :
In disp() method you are just registering your BroadcastRecever doesn't mean that your BroadcastReceiver is called. It will be called after sometime when your battery level will be changed.
Solution :
If you want to do something with yout BAT variable define a function in your Activity class and write whole logic inside it like
doThings(double batteryLevele){
//write whatever you want to do with BAT
}
and call this function from onReceive method of your BroadcastReceiver.