I just wanted to learn how to do Android application development, so I started with simple demos and now did something more complex (I think ;-)).
Usage: Eclipse + min SDK 8 + Android 2.2. Debugging and testing the emulator and my SGS I9000:
I managed to use AsyncTask to send some data (the Image I get from the camera or through the Gallery) to a server that works fine. So now that everything is working fine, I decided to add a custom notification to the status bar with a progress bar and text. Well, that worked too, but then I noticed:
1) as soon as I call publishProgress()from doInBackground(), which is called onProgressUpdate(), where I update my notification. I noticed that the "opening" and "closing" of the notification panel during the update, that it is not smooth. It freezes or someday you will see that the notification panel is no longer responding (if it is open, it will no longer close, or if it was closed and there will be no attempt to open it).
2) I also noticed that when I start x notifications, my system crashes and it looks like my system made a new start, which was not there!
Well, I thought I was doing everything in the docs, but I’m sure I did something wrong, because I’m sure that this is possible, what I’m looking for, since Facebook for Android is doing the same thing as in the near future when I select an image to publish.
onPreExecute() ( ).
, - , : ( , Okay )
NotificationHelper , /
public class NotificationHelper {
private Context mContext;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private RemoteViews contentView;
private static Random randomGenerator = new Random();
private int Id = -1;
public NotificationHelper(Context context)
{
mContext = context;
Id = randomGenerator.nextInt(1000);
}
public void CreateNotification(String text)
{
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotification = new Notification(R.drawable.icon, "MyTestApp", System.currentTimeMillis());
contentView = new RemoteViews(mContext.getPackageName(), R.layout.notification);
contentView.setProgressBar(R.id.progressBar, 100, 0, false);
contentView.setTextViewText(R.id.text, Html.fromHtml("<b>TEST </b>" + text));
contentView.setTextViewText(R.id.textStatus, String.format(mContext.getString(R.string.uploading), ""));
mNotification.contentView = contentView;
Intent notificationIntent = new Intent(mContext, main.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
mNotification.contentIntent = mContentIntent;
mNotificationManager.notify(Id, mNotification);
}
public void UpdateProgress(int Percent)
{
if (mNotification == null) return;
mNotification.contentView.setProgressBar(R.id.progressBar, 100, Percent, false);
contentView.setTextViewText(R.id.textStatus, String.format(mContext.getString(R.string.uploading), Integer.toString(Percent) + "%"));
mNotificationManager.notify(Id, mNotification);
}
public void Completed(boolean Status)
{
if (contentView != null) {
contentView.setViewVisibility(R.id.progressbarContainer, View.INVISIBLE);
if (Status)
{
contentView.setTextViewText(R.id.textStatus, mContext.getString(R.string.upload_done));
}else
{
contentView.setTextViewText(R.id.textStatus, mContext.getString(R.string.upload_failed));
}
mNotificationManager.notify(Id, mNotification);
}
}
}
:
public class MyTask extends AsyncTask<Void, Integer, Integer> {
private Context context;
private NotificationHelper pbNotificationHelper;
public String TextToShow;
public MyTask(Context context, String text) {
this.context = context;
TextToShow = text;
}
@Override
protected Integer doInBackground(Void... params) {
int xfileSize = 1048576 * 4;
if (true){
int i = 0;
while (true)
{
if (i > xfileSize) break;
i += 32 * 1024;
publishProgress( (i * 100) / xfileSize);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return 0;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pbNotificationHelper = new NotificationHelper(context);
pbNotificationHelper.CreateNotification(TextToShow);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
pbNotificationHelper.UpdateProgress(values[0]);
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
pbNotificationHelper.Completed(true);
}
}
:
public class main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void Test1Click(View view) {
new MyTask(getApplicationContext(), "This is a test message").execute();
}
}
, , !
, , publishProgress(), ! ? , ?
,
Cheers Gohlool