3

我只是想学习如何做一些 Android App 开发,所以我从一些简单的演示开始,现在做一些更难的事情(我猜 ;-))。

使用:Eclipse + min SDK 8 + Android 2.2。在模拟器和我的 SGS I9000 上调试和测试:

我设法使用 AsyncTask 将一些数据(我从相机或图库中获取的图片)发送到运行良好的服务器。因此,现在一切正常,我决定在状态栏中添加一个自定义通知,其中包含进度条和文本。好吧,这也有效,但后来我注意到:

1)一旦我打电话给publishProgress()我,我doInBackground()就会打电话给onProgressUpdate()我更新通知的地方。我注意到在更新期间“打开”和“关闭”通知栏并不顺畅。它冻结或有时您会看到通知栏不再响应(如果打开它将不再关闭,或者如果它已关闭并尝试打开它不会)。

2)我还注意到,当我启动 x Notifications 时,我的系统崩溃了,看起来我的系统重新启动了,但它没有!

好吧,我以为我按照文档中的规定做了所有事情,但我确信我做错了什么,因为我确信我正在寻找的可能是因为 Android 版 Facebook 与我选择图片时所做的一样分享。

我在onPreExecute()通话中创建通知(这意味着在 UI 线程上)。

如果有人能告诉我我的问题出在哪里,这里是我的代码:(我希望可以在这里发布代码) 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) {
             //mNotificationManager.cancel(Id);
             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) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pbNotificationHelper = new NotificationHelper(context);
        pbNotificationHelper.CreateNotification(TextToShow);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        // TODO Auto-generated method stub
        super.onProgressUpdate(values);
        pbNotificationHelper.UpdateProgress(values[0]);
    }

    @Override
    protected void onPostExecute(Integer result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pbNotificationHelper.Completed(true);
    }
}

这是我的测试活动:

public class main extends Activity {
    /** Called when the activity is first created. */
    @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()一切都冻结了,看起来一切都很顺利!有什么帮助吗?任何想法我做错了什么?

非常感谢, 干杯 Gohlool

4

1 回答 1

0

I think where ever you calling your async task code it should be in separate thread.

于 2011-05-16T13:26:16.853 回答