So, I have AsyncTask, which handles the upload to the server, at the same time calls the NotificationHelper class to update the notification panel to display the current percentage of download. This part is working fine, the only problems I am facing is getting the actions to work correctly and being able to use the intentions sent by the actions (the most recent attempt was to use the broadcast manager inside the parent activity, but I never get the message). If anyone could point me in the right direction, that would be greatly appreciated.
Here is some code:
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent("com.example.sibmedemo.sibme");
intent.putExtra("message","pause");
PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
mBuilder = new Notification.Builder(mContext)
.setContentTitle("Upload File")
.setContentText("Upload in progress")
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher,"Pause",pIntent)
.addAction(R.drawable.ic_launcher,"Cancel",pIntent);
mBuilder.setProgress(100,0,false);
mNotificationManager.notify(0,mBuilder.build());
, , , pendingIntents , .
------- -----------
-----------
public class FileUploadActivityV2 extends Activity {
public static int flags = 0;
public BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("message", "got a message");
if(intent.getStringExtra("message").equalsIgnoreCase("pause"))
flags = 1;
goAsync();
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_upload_activity_v2);
registerReceiver(receiver, new IntentFilter("sibme"));
flags = 0;
(new UploadTask(this)).execute();
}
, , brodcastReceiver, . - , goAsync() Android API , , onReceive, , .
AsyncTask ---------
@Override
protected Long doInBackground(Integer... integers) {
while(FileUploadActivityV2.flags == 0)
{
try{
Thread.sleep(1000);
Log.i("Running", "Running in background");
}catch(Exception e){
}
}
return null;
}
------
public void createNotification() {
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent("sibme");
intent.putExtra("message","pause");
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
mBuilder = new Notification.Builder(mContext)
.setContentTitle("Upload File")
.setContentText("Upload in progress")
.setSmallIcon(R.drawable.ic_launcher)
.addAction(R.drawable.ic_launcher,"Pause",pIntent)
.addAction(R.drawable.ic_launcher,"Cancel",pIntent);
mBuilder.setProgress(100,0,false);
mNotificationManager.notify(0,mBuilder.build());
}