現在、オンライン XML ファイルを解析し、AsyncTask を使用してデータをデータベースに挿入しようとしています。
AsyncTask が実行されると、ProgressDialog と通知バーを介して完了率をユーザーに通知します。
ユーザーは更新プロセス中にホームボタンを押して他のアプリにアクセスする可能性があるため、次のようなショートカットを開発したいと思います: ユーザーが通知バーに表示されたステータスをクリックすると、DataBaseUpdateService に直接戻ることができます。
以下は私が実装しようとして失敗したコードです。修正方法についてアドバイスをいただけますか?
データベース更新サービス
public class DataBaseUpdateService extends Activity {
Updatetask Task;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Task = new Updatetask(this.getApplicationContext());
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onPause()
{super.onPause();}
@Override
public void onResume()
{super.onResume();
if(Task.getStatus() == AsyncTask.Status.PENDING){Task.execute();}
}
public class Updatetask extends AsyncTask<Void, Integer, Void>
{ProgressDialog dialog;
private NotificationHelper mNotificationHelper;
private Context context;
public Updatetask_hymns(Context context){
this.context = context;
mNotificationHelper = new NotificationHelper(context);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mNotificationHelper.createNotification();
dialog =new ProgressDialog(DataBaseUpdateService.this);
dialog.setTitle("Updating DB...");
dialog.setMax(100);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected Void doInBackground(Void...unsed) {
***Code to parse xml and put in arraylist***
for(itemInsert=0;itemInsert<itemCount;itemInsert++)
{ ***insert data to database***
if(itemCount > 0) { publishProgress((int) ((itemInsert / (float) itemCount) * 100));}
}
return null;}
@Override
protected void onPostExecute(Void unused) {
mNotificationHelper.completed();
dialog.dismiss();
}
protected void onProgressUpdate(Integer... values) {
mNotificationHelper.progressUpdate(values[0]);
dialog.setProgress(values[0]);
}
}
}
通知ヘルパー
public class NotificationHelper {
private Context mContext;
private Notification mNotification;
private NotificationManager mNotificationManager;
private PendingIntent mContentIntent;
private CharSequence mContentTitle;
private int NOTIFICATION_ID = 1;
public NotificationHelper(Context context)
{
mContext = context;
}
/**
* Put the notification into the status bar
*/
public void createNotification() {
//get the notification manager
if(mNotificationManager==null){
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);}
//create the notification
int icon = android.R.drawable.stat_sys_download;
CharSequence tickerText = mContext.getString(R.string.database_update_main_action); //Initial text that appears in the status bar
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.database_update_title); //Full title of the notification in the pull down
CharSequence contentTitle = "My notification";
CharSequence contentText = "0% complete"; //Text of the notification in the pull down
Intent notificationIntent = new Intent(mContext,DataBaseUpdateService.class);
notificationIntent.addFlags(Notification.FLAG_ONGOING_EVENT);
mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, contentTitle, contentText, mContentIntent);
//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.FLAG_ONGOING_EVENT;
//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
/**
* Receives progress updates from the background task and updates the status bar notification appropriately
* @param percentageComplete
*/
public void progressUpdate(int percentageComplete) {
//build up the new status message
CharSequence contentText = percentageComplete + "% complete";
//publish it to the status bar
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
/**
* called when the background task is complete, this removes the notification from the status bar.
* We could also use this to add a new ‘task complete’ notification
*/
public void completed() {
//remove the notification from the status bar
mNotificationManager.cancel(NOTIFICATION_ID);
}
}