3

Android アプリの開発方法を学びたかっただけなので、簡単なデモから始めて、今はもっと難しいことをやっています (たぶん ;-))。

使用: Eclipse + min SDK 8 + Android 2.2。エミュレーターと SGS I9000 でのデバッグとテスト:

AsyncTask を使用して、いくつかのデータ (カメラまたはギャラリー経由で取得した画像) をうまく機能するサーバーに送信することができました。うまく機能するようになったので、進行状況バーとテキストを含むカスタム通知をステータス バーに追加することにしました。それもうまくいきましたが、次のことに気付きました。

1) 広告を呼び出すとすぐにpublishProgress()、通知を更新doInBackground()する場所を呼び出します。onProgressUpdate()更新中に通知バーを「開いたり閉じたり」して、スムーズではないことに気付きました。フリーズするか、通知バーが応答しなくなることがあります (開いている場合は閉じないか、閉じていて開こうとしても開きません)。

2) x Notifications を起動すると、システムがクラッシュし、システムが新しく起動したように見えますが、そうではありませんでした!

まあ、私はドキュメントにあるようにすべてをやったと思っていましたが、私は何か間違ったことをしたと確信しています.Facebook for Androidは私が写真を選ぶとすぐに同じことをするので、私が探しているものである可能性があると確信しています.シェア。

呼び出しで通知を作成しonPreExecute()ます (つまり、UI スレッドで)。

誰かが私の問題がどこにあるかを教えてくれるなら、ここに私のコードがあります: (ここにコードを投稿しても問題ないことを願っています)

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()興味深いのは、 nothings を呼び出さないとフリーズして、すべてがスムーズに見えることです。何か助けはありますか?私が間違っていることは何ですか?

よろしくお願いします、 乾杯 Gohlool

4

1 に答える 1

0

非同期タスクコードを呼び出す場所はどこでも、別のスレッドにする必要があると思います。

于 2011-05-16T13:26:16.853 に答える