Retrieving custom Future Android Ion by Koush - android

am new to Android. Sorry if this question is too simple. I have tried searching for a solution for weeks now. I am using Ion from https://github.com/koush/ion in my project. Uploads and downloads work well but when it comes to retrieving a specific custom Future after resuming the app I get stuck. I want to retrieve a single operation say an upload and stop it without affecting other uploads or vice versa for downloads.

The solution was to implement a broadcast receiver within creation of the process that returns a Future callback. This will allow you to cancel/stop its operation by just triggering the broadcast receiver
// set broadcast listeners
BroadcastReceiver broadcast = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String cancelled_msg_time = intent.getStringExtra("time");
// check if this download was cancelled
if (my_time.equals(cancelled_msg_time)) {
if (upload_started) {
// cancel asynctask
asycProcess.cancel(true);
}
// set flag as cancelled
cancelled = true;
}
}
};
// register the broadcast receiver to cancel downloads
IntentFilter intent_filter = new IntentFilter();
intent_filter.addAction("CANCEL_UPLOAD");
context.registerReceiver(broadcast, intent_filter);
Then your execution should somehow check status if it has been cancelled before proceeding
if (!cancelled) {
file_progress_handler = new FileProgressHandler();
// proceed with upload
}

Related

Android - auto redirect to new activity

I have a scenario when need to redirect to new activity automatically when the next user perform an action.
I need some idea, logic and suggest to find the best way to do it.
In more details.
This is like a bidding app. Where multiple service providers apply to the one job request.
User types:
Customer and Provider
The customer will select one of the bidder and hire him after completing the payment process (via braintree/paypal)
The system will send notification to the service provider that he won the project.
What I want is to redirect both of the Users. Customer and Service
Provider to be redirected automatically to the Current Job Activity
which will have google map and job details.
I know it can be done using notification click but I want it to be redirect automatically.
Can anyone please help me to share some idea or logic to perform such task?
no need of notification click you can automatically go into other activity once you receive the notification
String PUSH_NOTIFICATION = "pushNotification"
in onResume
LocalBroadcastManager.getInstance( this ).registerReceiver( receiver, new IntentFilter( Config.PUSH_NOTIFICATION ) );
in onPause
LocalBroadcastManager.getInstance( this ).unregisterReceiver( receiver );
private BroadcastReceiver receiver = new BroadcastReceiver()
{
#Override
public void onReceive( Context context, Intent intent )
{
if ( intent.getAction().equalsIgnoreCase( Config.PUSH_NOTIFICATION ) )
{
runOnUiThread( new Runnable()
{
#Override
public void run()
{
Intent i=new Intent (CurrentActivity.this,NavigateActivity.class)
startActivity(i);
}
} );
}
}
};

Can I see active/pending syncs in :sync process from main process?

Is it possible to determine (from the process an app was started in) sync status for a SyncAdapter that is running in a separate :sync process? I've been toying with the standard ContentResolver methods below and can't get any of them to return true unless the code below executes in the same process as my SyncAdapter (the :sync process).
val currentSyncs = ContentResolver.getCurrentSyncs().any { it.authority == <authority> }
val syncPending = ContentResolver.isSyncPending(account, <authority>)
val syncActive = ContentResolver.isSyncActive(account, <authority>)
None of the sync framework documentation seems to indicate that this isn't possible in cross-process scenarios, so I'm a bit stumped, but it seems like the most likely explanation for this.
I was rather unlucky trying to find a reliable way to get sync status information from ContentResolver.
What worked for me: Use intents to broadcast information from your SyncAdapter to another component:
// when sync started
Intent i = new Intent();
i.setAction(MyIntents.INTENT_ACTION_SYNC_STARTED);
context.sendBroadcast(i);
// when sync completed
Intent i = new Intent();
i.setAction(MyIntents.INTENT_ACTION_SYNC_DONE);
context.sendBroadcast(i);
and then receive the intents:
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, 'Sync started', Toast.LENGTH_LONG).show();
}
}, new IntentFilter(MyIntents.INTENT_ACTION_SYNC_STARTED));

Notify Activity in Android to refresh when data is updated

I have a background Service which updates the database in an Android App.
In the App this data is displayed, but I am having trouble finding a good way to trigger a refresh of the data.
Initially I just refreshed each time a view resumed, but that doesn't work obviously if the data is updated while the view is shown.
On iOS I use notifications, which I register in the view, and is triggered by the update process when completed. Is there a way to do something similar in Android to trigger an update on the UI thread from a background thread?
BroadcastReciever in your Activity is the best way to update Activity from the Service.
Activity:
BroadcastReceiver br = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
}
After that, all you need is to register some action for it in Activity:
public final static String BROADCAST_ACTION = "com.example.action";
IntentFilter intFilt = new IntentFilter(BROADCAST_ACTION);
registerReceiver(br, intFilt);
In your service you should build an intent and call sendBroadcast.
Service:
Intent intent = new Intent(MainActivity.BROADCAST_ACTION);
sendBroadcast(intent);
There's no need to use a BroadCastReceiver if you update your database and you're using a ContentProvider to saving your data to the db then whenever data change on the ContentProvider it should notify that data has changed and in your on your activity you use a CursorLoader then the Loader is notified and UI updated, this is very easy to do and has the advantage that a CursorLoader will run the thread on background see more here and here

Android, How to delay Broadcast Receiver for a while?

In my app there are 2 buttons. With one button; output is "A", with both buttons the output is "B".
I use broadcast receiver to get appropriate data. But there is a problem with pressing 2 buttons. Because i sometimes don't press each buttons at the same time. So receiver gets "A" firstly then "B". I think i can solve this problem with delaying receiver for a while. But how?
My broadcast code is below. I have tried Thread.sleep(100); but it doesn't work.
String word="";
IntentFilter intentFilter = new IntentFilter("android.intent.action.MAIN");
broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
try {
Thread.sleep(100);
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
word=intent.getExtras().getString("keyboard");
}
};
registerReceiver(broadcastReceiver, intentFilter);
Instead of adding a delay in the Broadcast receiver, create a delay before sending the broadcast.
In the button handlers, use some boolean variables to check button clicks, and use handler.postDelayed
to call sendBroadcast. You can also add some checks to make sure that only one broadcast is sent during subsequent clicks
You can not operate on two intents inside a BroadcastReceiver. The way it works is that you receive one intent, you perform your operation on it (serially) and then finish it until you receive next intent. Your solution does not work because you put the thread in sleep, and in this duration of course you can't get the next bcasted intent either, until the sleep period is over, which then you finish processing the current intent, and then you receive the next one.
I am not sure what is your scenario, but you need to somehow keep the state in your code, so your code knows you already have received the first intent. For example you can define a variable outside of broadcastReceiver, which keeps track of the intents received so far, and then inside the receiver, you can check the state, and based on whether input A already is received or not you can operate.
You can send broadcast delayed, like use this wrap function.
public static void sendBroadcastDelayed(Context context, Intent intent, long delay) {
Handle handler = new Handle();
handler.postDelayed(new Runnable() {
#override
public void run() {
context.sendBroadcast(intent);
}
}, delay);
}

send broadcast with combination of localbroadcastmanager sendorderedbroadcast

I'm wanting to implement what CommonsWare describes on this blog post: http://commonsware.com/blog/2010/08/11/activity-notification-ordered-broadcast.html. The post makes sense, and I was able to browse the example source here: https://github.com/commonsguy/cw-advandroid/tree/master/Broadcast.
What I'm curious about is if calling LocalBroadcastManager.getInstance(UnzipService.this).sendBroadcast(broadcast); inside of a service will still be picked up by a broadcast receiver of the type you define in your manifest.
In case what I'm asking isn't clear, what I'm trying to do is use the LocalBroadcastManager because the broadcasts from my service don't necessarily need to be seen system wide and I'd rather keep them private if possible, but I also want to display notifications if the user closes my app and the service is still running. Is there a way to combine both of those capabilities without sending a broadcast twice inside of the service?
(What I don't want to have to do) like:
LocalBroadcastManager.getInstance(UnzipService.this).sendBroadcast(broadcast);
sendOrderedBroadcast(broadcast);
What I'm curious about is if calling LocalBroadcastManager.getInstance(UnzipService.this).sendBroadcast(broadcast); inside of a service will still be picked up by a broadcast receiver of the type you define in your manifest.
No. LocalBroadcastManager only works with receivers registered with the LocalBroadcastManager singleton itself. Moreover, LocalBroadcastManager does not support ordered broadcasts, last I checked.
what I'm trying to do is use the LocalBroadcastManager because the broadcasts from my service don't necessarily need to be seen system wide and I'd rather keep them private if possible
So long as you are not using an <intent-filter> on your BroadcastReceiver in the manifest, and therefore are using an explicit Intent as the broadcast itself, your broadcast will only be seen by yourself and the bit of the OS that manages broadcasts. Other apps will not be able to spy upon it.
If you only have 2 objects that might handle your broadcast (in your case an Activity and a notifications controller), you can achieve the behavior of a ordered broadcast using only the LocalBroadcastManager.
The general idea is:
Set up your Service so that it broadcasts an Intent to your Activity with a particular action when you want to display your result
In your Activity create a BroadcastReceiver that handles your Service result Intent, and register it on the LocalBroadcastManager with an IntentFilter using the action from step 1
In your Service, when the results are available, try to send the result Intent using LocalBroadcastManager.getInstance(Context).sendBroadcast(Intent) this method returns a boolean that indicates if the broadcast has been sent to at least one receiver. If this boolean is false, it means that your Activity didn't handle your broadcast and you should show a notification instead.
In your service:
public UnzipService extends IntentService {
public static final String ACTION_SHOWRESULT = UnzipService.class.getCanonicalName() + ".ACTION_SHOWRESULT";
#Override
protected void onHandleIntent(Intent intent) {
Thread.sleep(500); // Do the hard work
// Then try to notify the Activity about the results
Intent activityIntent = new Intent(this, YourActivity.class);
activityIntent.setAction(ACTION_SHOWRESULT);
activityIntent.putExtra(SOME_KEY, SOME_RESULTVALUE); // Put the result into extras
boolean broadcastEnqueued = LocalBroadcastManager.getInstance(this).sendBroadcast(activityIntent);
if (!broadcastEnqueued) { // Fallback to notification!
PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
.notify(SOME_ID, new NotificationCompat.Builder(this)
.setContentIntent(pendingIntent)
.setTicker("results available")
.setContentText("results")
.build());
}
}
}
In your Activity:
public YourActivity extends Activity {
private BroadcastReceiver resultReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
processResult(intent); // Results Intent received through local broadcast
}
}
private IntentFilter resultFilter = new IntentFilter(UnzipService.ACTION_SHOWRESULT);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate();
Intent intent = getIntent();
if (UnzipService.ACTION_SHOWRESULT.equals(intent.getAction())) {
// The Activity has been launched with a tap on the notification
processResult(intent); // Results Intent contained in the notification PendingIntent
}
}
#Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this)
.registerReceiver(resultReceiver, resultFilter);
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(resultReceiver);
super.onPause();
}
private void processResult(Intent intent) {
// Show the results from Intent extras
}
}
This should be a complete working example.
I hope this helps who is trying to implement ordered broadcasts with LocalBroadcastManager from support library!
I understand you want to achieve the following:
"I have an event that occurs in the background. I want to update my activity, if the activity is on the screen. Otherwise, I want to raise a Notification." (#TheCommonsBlog)
You can achieve this behaviour by implementing a ResultReceiver.
Examples Restful API service and
http://itekblog.com/background-processing-with-intentservice-class/
What you basically do is instance a ResultReceiver in your Activity and pass it to the Service like a Parcelable parameter through an intent. Then, each time your service whats to update the UI, the service verifies the ResultReceiver object for NULL. If not NULL, you update the Ui via the onReceiveResult interface. Else, you raise a notification. When your activity dismisses, make sure you set the ResultReceiver on the Service to NULL.
Hope it helps.
PS: IMO, broadcasts are too much work and hard to control.
Use LocalBroadcastManager and broadcasts become easy to use.
I am not in favor of updating an Activity if an event occurs in the background. The user might already be doing something else in the Activity. Seems to me that a Notification is sufficient; it's always visible and remains until the user dismisses it. Gmail and Gcal work like this; Gmail doesn't update the current screen if a new mail comes in. If you want to know how to handle the task flow for handling a notification when the user is already in the app, see the Notifications API guide and also the [Notifying The User2 training class.

Categories

Resources