2015-10-15 86 views
0

我需要更改此應用程序,以便它使用Handler而不是AsyncTask來運行虛假下載。我一直在困擾它。我曾嘗試使用示例來解決它,但我認爲通知線程讓我困惑。做我的頭英寸Android Studio將異步任務更改爲處理程序

public class Responsive extends AppCompatActivity { 

// 5. Declare and populate a string array for the ListView items (put this directly underneath the class declaration) 
private static final String[] LIST_ITEMS = new String[]{"one", "two", "three", "four"}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_responsive); 

    // 4. Call setupButton() and setupListView() from the onCreate method. 
    setupButton(); 
    setupListView(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_responsive, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

// 3. Open Responsive.java for editing and add the following three methods to it (don’t get rid of any of the existing code): 

private void setupListView() { 
    ListView lv = (ListView) findViewById(R.id.listView); 
    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, LIST_ITEMS)); 
} 

private void setupButton() { 
    Button button = (Button) findViewById(R.id.notificationButton); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      createNotification(); 
     } 
    }); 
} 

private void createNotification() { 
    new Downloader(getApplicationContext()).execute(0); 
} 
} 

回答

-1
public class NotificationHelper { 

    private Context mContext; 
    private int NOTIFICATION_ID = 1; 
    private NotificationCompat.Builder mNotification; 
    public NotificationManager mNotificationManager; 
    private PendingIntent mContentIntent; 
    private CharSequence mContentTitle; 
    public NotificationHelper(Context context) 
    { 
     mContext = context; 
    } 

    /** 
    * Put the notification into the status bar 
    */ 
    public void createNotification() { 
//get the notification manager 
     mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
     mContentTitle = mContext.getString(R.string.content_title); 
//create the notification 
     mNotification = new NotificationCompat.Builder(mContext) 
       .setSmallIcon(R.drawable.ic_action_download) 
       .setContentTitle(mContentTitle) 
       .setTicker(mContext.getString(R.string.download_ticker)) 
       .setOngoing(true); 

//Text of the notification in the notification drawer 
     CharSequence contentText = "0% complete"; 
     mNotification.setContentText(contentText); 
//we are not going to use the pending intent,so just create a blank one 
     Intent notificationIntent = new Intent(); 
     mContentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0); 
     mNotification.setContentIntent(mContentIntent); 
//show the notification 
     mNotificationManager.notify(NOTIFICATION_ID, mNotification.build()); 
    } 

    /** 
    * Receives progress updates from the background task and updates *the notification bar 
    * @param percentageComplete 
    */ 
    public void progressUpdate(int percentageComplete) { 
     //build up the new status message 
     CharSequence contentText = percentageComplete + "% complete"; 
     //publish it to the notification 
     mNotification.setContentText(contentText); 
     mNotificationManager.notify(NOTIFICATION_ID, mNotification.build()); 
    } 

    /** 
    * called when the background task is complete, this removes the *notification. 
    */ 
    public void completed(){ 
     //remove the notification from the status bar 
     mNotificationManager.cancel(NOTIFICATION_ID); 
    } 

}