3分钟
4.4实施步骤
实施步骤
步骤1:新建项目,命名为download。
步骤2:修改MainActivity.java,如表表4-4-1所示。
表4-4-1 MainActivity.java
package com.example.download;
import android.app.NotificationManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final int TYPE_Progress = 1;
private Intent service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
service = new Intent(this,DownloadService.class);
}
public void onClick(View v){
switch (v.getId()){
case R.id.btn1:
startService(service);
break;
default:
break;
}
}
}步骤3:写一个Service完成下载任务,并向Notification传递下载进度,Service功能在DownloadService.java中。如表4-4-2所示。
表4-4-2 DownloadService.java
package com.example.download;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
public class DownloadService extends Service {
private Handler mHandler;
private int progress = 0;
private NotificationManager manger;
private Runnable runnable;
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
mHandler = new Handler(getMainLooper());
manger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
runnable = new Runnable() {
@Override
public void run() {
if(progress>99){
progress=0;
manger.cancel(MainActivity.TYPE_Progress);
}else{
sendNotification();
progress++;
mHandler.postDelayed(runnable,500);
}
}
};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if(intent==null){
return super.onStartCommand(intent, flags, startId);
}
int command = intent.getIntExtra("command",0);
if(command==1){
progress=0;
mHandler.removeCallbacks(runnable);
manger.cancel(MainActivity.TYPE_Progress);
}else {
if (progress < 1) {
mHandler.post(runnable);
}
}
return super.onStartCommand(intent, flags, startId);
}
private void sendNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.push));
//禁止用户点击删除按钮删除
builder.setAutoCancel(false);
//禁止滑动删除
builder.setOngoing(true);
builder.setShowWhen(false);
builder.setContentTitle("下载中..."+progress+"%");
builder.setProgress(100,progress,false);
builder.setOngoing(true);
builder.setShowWhen(false);
Intent intent = new Intent(this,DownloadService.class);
intent.putExtra("command",1);
Notification notification = builder.build();
manger.notify(MainActivity.TYPE_Progress,notification);
}
@Override
public void onDestroy() {
mHandler.removeCallbacks(runnable);
manger.cancel(MainActivity.TYPE_Progress);
super.onDestroy();
}
}步骤4:修改布局文件activity_main.xml,该布局只有一个按钮,点击后开始下载进度。如表4-4-3所示。
表4-4-3 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.download.MainActivity">
<Button android:id="@+id/btn1"
android:onClick="onClick"
android:text="下载"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>步骤5:要启用Service,必须向系统声明该Service,需要在AndroidManifest.xml中注册Service 。如表4-4-4所示。
表4-4-4 AndroidManifest.xml
<service
android:name=".DownloadService"
android:enabled="true"
android:exported="true" />步骤6:在手机上运行并观察效果。
学员评价