Using the GCM service to communicate between 2 applications - java

I'm trying to create 2 applications that communicate by the GCM service.
Let's assume that I'm trying to sends string from app A to B and then from B to A.
I'm very new to the GCM service and I'm alittle bit confused. Every time you see myApiCode I replaced it in my original code with the api code.Here is the A code:
public class MainActivity extends Activity
{
private final String myApiKey = "903137756997";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, "myApiCode");
} else {
Log.v("INFORMATION", "Already registered");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And here is the GCMIntentService:
public class GCMIntentService extends GCMBaseIntentService
{
private final String myApiKey = "903137756997";
public GCMIntentService()
{
super("123");
}
...
#Override
protected void onMessage(Context arg0, Intent arg1)
{
Log.d("GCM", "RECIEVED A MESSAGE");
System.out.println("123123123123");
}
...
}
That code that I've attached would be app A and now I will attach the code of app B:
The following code is a service called from the main activity:
public void onCreate()
{
super.onCreate();
Sender sender = new Sender(myApiKey);
Message message = new Message.Builder().build();
Result result = null;
try {
result = sender.send(message, "123", 5);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (result.getMessageId() != null) {
String canonicalRegId = result.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration ID: update database
}
} else {
String error = result.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister database
}
}
}
I have to mention that both of the apps are running without exceptions but looks like nothing happens.. I guess I have done something wrong with the keys because I still can't understand how app B will find app A.

You have to override the onRegistered method in GCMIntentService. This will be called when the GCM server returns a registration ID that was prompted by your call to GCMRegistrar.register.
Your implementation of this method should upload the String argument to a server you control, and the server can then send out messages targetting the ID you uploaded.
Also you should not push messages directly between apps this way, because it will require you to ship your private API key out in the app package in order to send messages.

Related

How can I send an event from MainActivity.java to Javascript in React Native?

Currently the following code does successfully get the text that is being shared to the app, but I am having trouble finding out how to send this event to the Javascript side. I tried creating a new NavigationReactGateway but my app just crashes. I'm new to Java so I don't even know if I did it properly.
public class MainActivity extends SplashActivity {
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
protected String getMainComponentName() {
return "MyApplication";
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent);
}
}
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Send event to Javascript as "share" event
}
}
}
What is the correct way to send this event to Javascript?
The app has react-native-navigation and there are a lot of references to an event emitter, I'm just unsure how to get the react context to emit the event. If that makes any sense.
I was able to modify the handleSendText method to the following to send an event.
private void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
ReactContext context = NavigationApplication.instance.getReactGateway().getReactContext();
if (context != null) {
WritableMap params = Arguments.createMap();
params.putString("event", sharedText);
context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit("share", params);
}
}
}
I also had to change from using the onCreate method to using onStop, then the react context has been initialized and my event launches whether the app was already open or not.
You can use webView.addJavascriptInterface()
You can find documentation here.

Transport Controls not Updating after MediaPlayerService is destroyed

Okay. I have been struggling with this issue for a while.
Since the latest Wear OS upgrade, a play/pause transport control is displayed in the notification panel instead of showing media controls on a notification. My media player service works fine until it has been paused for some time and android destroys it as it has moved to the background. If the user selects the play button from the transport controls, the music starts playing as it should, but the transport controls no longer update meaning the user can only hit play and can no longer pause. This can be very frustrating if they need to stop audio and can't.
Here is the code where I initialize the media session. This is called at onCreate
private void initMediaSession() {
if (mediaSessionManager != null) return;
mediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
//Create a new mediaSession
mediaSession = new MediaSessionCompat(getApplicationContext(), "com.turndapage.navmusic.mediaplayer");
//Get mediaSessions Transport Controls
mediaSession.getController().getTransportControls();
//Set Mediasession ready to receive media commands
mediaSession.setActive(true);
mediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
callback = new MediaSessionCompat.Callback() {
// Implement callbacks
#Override
public void onPlay() {
super.onPlay();
Cat.d("Playing from transport controls.");
// This is the event that is triggered from the transport controls
resumeMedia();
}
#Override
public void onPause() {
super.onPause();
Cat.d("Pausing from transport controls");
pauseMedia();
}
#Override
public void onSkipToNext() {
super.onSkipToNext();
skipToNext();
}
#Override
public void onSkipToPrevious() {
super.onSkipToPrevious();
skipToPrevious();
}
#Override
public void onStop() {
super.onStop();
stop();
}
#Override
public void onSeekTo(long position) {
super.onSeekTo(position);
mediaPlayer.seekTo((int)position);
}
#Override
public void onFastForward() {
super.onFastForward();
fastForward();
}
#Override
public void onRewind() {
super.onRewind();
rewind();
}
#Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
int id = Integer.parseInt(mediaId.replace("song", ""));
int thisIndex = 0;
for(Song song : songLibrary.getSongs()) {
if(song.getID() == id) {
ArrayList<Song> songs = getCurrentList();
thisIndex = songs.indexOf(song);
break;
}
}
int[] ids = new int[getCurrentList().size()];
for(int i = 0; i < ids.length; i++) {
ids[i] = getCurrentList().get(i).getID();
}
startPlaying(ids, thisIndex);
}
#Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
KeyEvent event = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
Cat.e("onMediaButtonEvent called: " + event);
if (event.getAction() == KeyEvent.ACTION_UP) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PAUSE:
pauseMedia();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
playMedia();
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
//skipToNext();
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
//skipToPrevious();
break;
}
}
return super.onMediaButtonEvent(mediaButtonEvent);
}
};
// Attach a callback to receive MediaSession update
mediaSession.setCallback(callback);
setSessionToken(mediaSession.getSessionToken());
}
This method is called when the user clicks the play transport control
public void resumeMedia() {
wasPlaying = true;
if(mediaPlayer != null) {
try {
if (!getPlaying()) {
mediaPlayer.seekTo(resumePosition);
mediaPlayer.start();
sendPlayPauseUpdate(true);
buildNotification(true);
}
ComplicationHelper.updatePlayPause(this, true);
} catch (IllegalStateException ex) {
ex.printStackTrace();
//restoreState();
startMediaPlayer();
}
}
}
the send play/pause update just sends a broadcast to the activity to update controls there.
Inside the notification code, I have this to update the playback state of the media session. As you can see, the pause or play actions are only added based on whether the media player is playing or not. Despite this, the play action is still displayed:
long actions = MEDIA_SESSION_ACTIONS;
if(isPlaying)
actions |= PlaybackStateCompat.ACTION_PAUSE;
else
actions |= PlaybackStateCompat.ACTION_PLAY;
mediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
.setActions(actions)
.setState(isPlaying ? PlaybackStateCompat.STATE_PLAYING :
PlaybackStateCompat.STATE_PAUSED, resumePosition, 1)
.build());
and here are the media session actions:
// Media Session Actions
private static final long MEDIA_SESSION_ACTIONS =
PlaybackStateCompat.ACTION_SEEK_TO |
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH |
PlaybackStateCompat.ACTION_PREPARE |
PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH |
PlaybackStateCompat.ACTION_PLAY_PAUSE |
PlaybackStateCompat.ACTION_STOP |
PlaybackStateCompat.ACTION_FAST_FORWARD |
PlaybackStateCompat.ACTION_REWIND;
What I have tried:
Changing media session to static and back
making the callback a variable rather than a making a new one when created.
Manually setting the mediasession's playback state to playing after play from transport controls
This works find on the phone app although I am using a pending intent to send a start command to run the resume function rather than transport controls. I am also able to resume playback from the watch's transport controls if I am using the phone to play audio and the controls update correctly even though the phone app is using the exact same class.
Please let me know if any other information could be helpful.
Thanks!
-Joel

Scan for iBeacon upon receiving remote notification

I currently have an app setup to receive remote notifications using Azure Notification Hub.
Now, I would like to scan for iBeacons, see if a specific one is close by and if so, the notification should not be shown to the user. However, if the beacon isn't in sight, the user should receive this notification.
Basically I want the beacon to supress the notifications for this app.
How would one go about doing this?
Based on the docs from Azure, when a remote notification comes in, you get a callback like this:
public class MyHandler extends NotificationsHandler {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
#Override
public void onReceive(Context context, Bundle bundle) {
ctx = context;
String nhMessage = bundle.getString("message");
sendNotification(nhMessage);
if (MainActivity.isVisible) {
MainActivity.mainActivity.ToastNotify(nhMessage);
}
}
private void sendNotification(String msg) {
// put your notification code here
...
}
}
If you want to filter the notifications based on what beacons are present, you can add that logic to the onReceive method like this:
public void onReceive(Context context, Bundle bundle) {
if (!(MyApplication)this.getApplication()).isBeaconVisible()) {
// Suppress notification by returning early from method
return;
}
...
}
The above isBeaconVisible() could be implemented in a custom Android Application class using the Android Beacon Library with something like below. You'll need to read more about how to set up that library to make this work. You'll also need to register the custom Application class in your AndroidManifest.xml.
public class MyApplication extends Application implements BeaconConsumer, RangeNotifier {
public Collection<Beacon> mVisibleBeacons;
public void onCreate() {
super.onCreate();
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
// TODO: look up the proper I_BEACON_LAYOUT in a google search
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(I_BEACON_LAYOUT));
beaconManager.addRangeNotifier(this);
}
#Override
public void onBeaconServiceConnect() {
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
try {
beaconManager.startRangingBeaconsInRegion(new Region("all-beacons", null, null, null));
} catch (RemoteException e) {
e.printStackTrace();
}
}
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
mVisibleBeacons = beacons;
}
public boolean isBeaconVisible() {
return mVisibleBeacons.size() > 0;
}
}
The above logic for isBeaconVisible() returns true if any beacon with any identifier has been seen in the last second. But you can alter this to make it more sophisticated per your requirements.
You can use some opensource library to work with beacons. I used Altbeacon library for example.
Here is the samples : https://altbeacon.github.io/android-beacon-library/samples.html
For your target you need to implement BeaconConsumer interface on Activity or Service. It has a method onBeaconServiceConnect(). Example of implementation:
#Override
public void onBeaconServiceConnect() {
beaconManager.addRangeNotifier(new RangeNotifier() {
#Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
if (beacons.size() == 0) {
Log.i(TAG, "Show your notification here");
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("someRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}

Not receiving data in definite chunks in android from arduino in serial communication over USB

I am making an android application that can communicate with arduino serially over USB and used UsbSerial library from Github.
I'm receiving data from arduino in UsbReadCallback which I have implemented as:
private UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
#Override
public void onReceivedData(byte[] arg0) {
try {
String data = new String(arg0, Charset.forName("utf-8"));
if (mHandler != null)
{
mHandler.obtainMessage(0, data).sendToTarget();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
and my Handler as:
private static class MyHandler extends Handler {
private final WeakReference<SensorDisplayActivity> mActivity;
public MyHandler(SensorDisplayActivity activity) {
mActivity = new WeakReference<>(activity);
}
#Override
public void handleMessage(Message msg) {
String data = ( String) msg.obj;
mActivity.get().mainValue_tx.setText(data);
}
}
But problem arriving when I'm displaying it in the TextView using setText() its not showing the correct value i.e. sometimes showing 00 in place of 100 and 2 in place of 42 etc. but when I'm using append() method of TextView the correct values are appending correctly. What might be the solution?

Where to put in Android code to communicate with server over http

The question is how to communicate with an Android phone to a server, so that if the Activity is left and the call in the Activity was not successful to repeat the transaction once again automatically. Just now I use the AsyncTask of Android to communicate with the server:
new AsyncTask<String, Void, List<String>>() {
#Override
protected void onPreExecute(
showWaitDialog();
}
#Override
protected void onPostExecute(List<String> msgList) {
//here I put the handling after the POST ie. error and success handling
hideWaitDialog();
if (msgList.isEmpty() {
//success handling --> starting an new Activity
} else {
errorView.setText (...);
errorLayout.setVisibility (View.VISIBLE);
}
}
#Override
protected List<String> doInBackground(String... params) {
List<String> msgs = new ArrayList<String>();
try{
//for example submitting an JSONObject
JSONObject result = HttpUtils.sendHttpPost(
AppConstants.WEB_URL, jsonObject);
//error handling on the result
boolean hasErrors = JsonResult.isOk(result);
if (hasErrors) {
// adding errors to msgs list
String[] errorMessages = JsonResult.getErrorMessages (result,...);
fillList (msgs, errorMessages);
return msgs;
}
} catch (CommunicationError er) {
msgs.add (er...);
}
return msgs;
}
}
The problem with this approach is, that if I don't have a successful transmission of the data I must stay in the same Activity. Until now I show an error message to the user and he is in charge to submit by a button again the results to the server.
What I'm looking for is some Activity that remains persistent in the memory which runs later in the case that the transmission wasn't made.
As an application case I use this to dynamically upload pictures for a Waypoint in a map if I pressed that waypoint. In some case it can happens that the connection to the mobile service provider isn't available (mountains, forest, far apart from antenna). Then I want to leave the map Activity and switch to the detail view of this waypoint. In the success case I put the picture into my model classes and make an serialization. If the user clicks again on the same waypoint the picture is not loaded again. In the non success case I don't want to wait that the user clicks against on the waypoint to retrieve the image. In fact I need a background task, some sort of a queue that pictures of waypoints that are already visited on couldn't be retrieved are loaded until the communication part gives back a positive result and the image can be written into the model. The next time the user is pressing the Waypoint the picture will be then present.
Are there any best practices for making such a code implementation?
Is there any example around?
Is there a better way of doing this?
Yes, you need to Implement Intent Service for this requirement
According to the developers website
The IntentService class provides a straightforward structure for running an operation on a single background thread.
For complete details and working source code, Go through the Android Docs
Thanks to the answer of David.
I just read after the suggestion the tutorial at
[1] http://code.tutsplus.com/tutorials/android-fundamentals-intentservice-basics--mobile-6183
After my tests I prefered a Service (not an IntentService)
and created a service: SubmissionService
public class SubmissionIntentService extends Service {
private List<PendingMessage> pMsgList = new CopyOnWriteArrayList<PendingMessage>();
private Handler handler = new Handler();
private boolean hasAppStopped = false;
private Runnable runner;
public SubmissionIntentService() {
super();
Log.d (TAG, "Service created...");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
PendingMessage pMessage = (PendingMessage) intent.getParcelableExtra(AppConstants.MESSAGE_OBJECT);
synchronized (pMsgList) {
pMsgList.add(pMessage);
}
if (runner == null) {
handler.postDelayed(runner = initializeRunnable(), 500);
}
return Service.START_NOT_STICKY;
}
private void runAsLongAppIsActive (Runnable runner) {
if (!hasAppStopped) {
handler.postDelayed (runner, SOME_INTERVAL_CONSTANT);
}
}
private Runnable initializeRunnable() {
Runnable result;
result = new Runnable() {
#Override
public void run() {
if (pMsgList.isEmpty()) {
runAsLongAppIsActive (this);
return;
}
PendingMessage[] pMArray = null;
synchronized(pMsgList) {
pMArray = pMsgList.toArray (new PendingMessage[pMsgList.size()]);
}
if (pMArray==null || pMArray.length==0) {
runAsLongAppIsActive (this);
return;
}
Log.d (TAG, "Message List size is actually :"+pMArray.length);
for (PendingMessage pM: pMArray) {
try {
JSONObject jsonMess = JSONSendMessage.buildOutput (pM);
JSONObject result = HttupUtils.sendHttpPost (WEB_URL, jsonMess);
boolean hasErrors = JSONResult.isOk (result);
if (hasErrors) {
//TODO: error handling in case of transmission
//don't remove the message from the queue
runAsLongAppIsActive(this);
return;
}
//remove pending transmission of the queue if success
synchronized (pMsgList) {
pMsgList.remove (pM);
}
//inform over receiver if activity is shown
Intent broadcastIntent = new Intent();
//put data in intent
sendBroadcast (intent);
//more important
WayPointModel model = ModelInstance.getWayPointModel();
model.addToModel (pM, result);
model.store();
} catch (Exception e) {
continue; //try to send other messages
}
}
runAsLongAppIsActive (this);
}
};
return result;
}
}
#Override
public void onDestroy() {
hasAppStopped = true;
handler.removeCallbacks (runner);
super.onDestroy();
}
}
Further I added a ResponseReceiver:
public class ResponseReceiver extends BroadcastReceiver {
public static final String ACTION_RESP = "MESSAGE_PROCESSED";
#Override
public void onReceive(Context context, Intent intent) {
//work in progress...
}
}
and in the Activity where I want to be informed about events:
public class SomeActivity extends Activity {
private ResponseReceiver receiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new ResponseReceiver();
registerReceiver(receiver, filter);
...
}
}
and finally to send messages over Http:
Intent msgIntent = new Intent(this, SubmissionIntentService.class);
msgIntent.putExtra(...);
startService(msgIntent);
don't forget to declare the service in your manifest:
<service android:name="ch.xxx.app.service.SubmissionIntentService" />
Observations:
- I called the method startService(...) from different Activities. The constructor is only called once.
==> I have just on instance of the service for all Activities (exactly what I need).
What I don't get until now:
- Putting back data to the Activity. What is if the Activity is at the moment no shown?

Categories