2015-10-15 43 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); 
    } 

}