1

目前,我正在尝试使用 AsyncTask 解析在线 XML 文件并将数据插入数据库。

当执行 AsyncTask 时,我将通过 ProgressDialog 以及通知栏通知用户完成百分比。

由于用户在更新过程中可能会按下home键并访问其他应用程序,所以我想开发一个这样的快捷方式: 当用户点击通知栏中显示的状态时,他们可以直接回到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);
    }
}
4

1 回答 1

0

维护一个标志以找出您的活动是在后台还是前台,然后如果它在后台显示通知并在单击它时启动您的活动的挂起。

if(inBackGround){
    Intent contentIntent = new Intent(this,DataBaseUpdateService.class);
    PendingIntent contentPendingIntent = PendingIntent.getActivity(
                    this, 0, contentIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent.getBroadcast(this, 0, contentIntent , PendingIntent.FLAG_UPDATE_CURRENT);
    String notificationTitle ="sfdf";
    String notificationText = "dsf";
    Notification notification = new Notification(
            notificationIcon, notificationTitle, System.currentTimeMillis());
    notification.setLatestEventInfo(this, notificationTitle, notificationText, contentIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Trigger the notification.
    mNotificationManager.notify(0, notification);
}
于 2012-06-10T10:47:38.063 回答