通知栏Notification实现推送下载
目标
- 点击button通知栏出现通知
- 下拉菜单显示具体信息
- 点击推送信息进行下载
- 下载完成之后进行安装
实现方式
- 广播Broadcast
- 服务Service
- 网络请求框架
效果预览
具体实现
1. 这里我是用的网络请求框架是aFinal的FinalHttp,首先我们需要添加依赖,我这里我是通过jar包的方式添加的,这个都无所谓的,建议是添加依赖.
compile files('libs/afinal_0.5.1_bin.jar')
2.把相关权限一定要加上的,比如网络权限、推送是的震动权限……
//震动的权限
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
//网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
//浮窗权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"></uses-permission>
3.注册广播与注册服务
<receiver android:name=".CustomReciver">
<intent-filter>
<action android:name="com.intent.click_notification"></action>
</intent-filter>
</receiver>
<service android:name=".CustomService">
<intent-filter>
<action android:name="com.service.notification"></action>
</intent-filter>
</service>
4.Notification的推送
String action="com.intent.click_notification";
Intent intent = new Intent(action);
//PendingIntent主要用来处理即将发生的事,相当于Intent的延时,在这里是用来发送广播通知
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Notification notification = builder
.setContentTitle("更新通知")//标题
.setContentText("QQ3.233已正式发布请您及时更新,\n新版本将为您带来不一样的体验!")//内容
.setWhen(System.currentTimeMillis())//通知时间,系统时间
.setSmallIcon(R.mipmap.notification)//标题栏上显示的通知icon
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))//通知显示的icon
.setDefaults(Notification.DEFAULT_VIBRATE)//DEFAULT_VIBRATE默认震动,DEFAULT_SOUND默认声音,DEFAULT_LIGHTS默认灯光
.setColor(Color.parseColor("#98903B"))//smallIcon的背景色
.setContentIntent(pendingIntent)
.build();
manager.notify(1, notification);
这样一个通知就发送成功了,通知栏上也会显示出SmallIcon图标,之后是进行点击通知进行下载处理,首先Notification发送一个Broadcast广播,但是广播里面不能做耗时操作,所以在广播里开启一个Service服务,通过在Service服务里进行下载操作,当然我们都知道Service不是一个独立的线程也不是一个独立的进程,它是完全存在于主线程里,所以下载要在多线程里面处理,这里我们使用框架处理就方便多了.
1. 广播Broadcast CustomReciver.java
/**
Created by TangRen on 2016/7/13.
*/
public class CustomReciver extends BroadcastReceiver {
private String action = "com.service.notification";
private Intent startService;
@Override
public void onReceive(Context context, Intent intent) {
startService = new Intent(context, CustomService.class);
startService.setAction(action);
context.startService(startService);
}
}
2.Service服务 CustomService.java
/**
* Created by TangRen on 2016/7/13.
*/
public class CustomService extends Service {
FinalHttp http;
HttpHandler<File> handler;
private String action = "com.service.notification";
private ProgressDialog dialog;
int pr = 0;
@Override
public IBinder onBind(Intent intent) {
System.out.print("--------------------------------------");
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("onCreate", "服务已开始-------------------");
String url = "http://gdown.baidu.com/data/wisegame/4ae6d2d7378e6cdf/QQ_122.apk";
final String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/qq.apk";
File file = new File(savePath);
if (file.exists()) {
file.delete();
}
http = new FinalHttp();
handler = http.download(url, new AjaxParams(), savePath, true, new AjaxCallBack<File>() {
@Override
public void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(), "开始下载", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoading(long count, long current) {
super.onLoading(count, current);
if (current != count && current != 0) {
pr = (int) (current / (float) count * 100);
} else {
pr = 100;
}
Toast.makeText(getApplicationContext(), "下载进度" + pr + "%", Toast.LENGTH_SHORT).show();
Log.d("msg", "速度:" + pr + "%");
// progressBar.setProgress(pr);
}
@Override
public void onSuccess(File file) {
super.onSuccess(file);
stopSelf();
Toast.makeText(getApplicationContext(), "下载成功!", Toast.LENGTH_SHORT).show();
NotificationManager manager = (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
manager.cancelAll();
File file_path=new File(savePath);
if (file_path.getName().endsWith(".apk")){
Intent install=new Intent();
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.setAction(android.content.Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(file_path), "application/vnd.android.package-archive");
startActivity(install);
}
}
@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
stopSelf();
Toast.makeText(getApplicationContext(), "下载失败!", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("onDestroy", "服务已停止-------------------");
}
}
这样所有工作就做完了,因为我这里下载进度用的是吐司的方法所以下载进度有偏差,可能吐司显示已下载70%这时系统已经提示你安装程序了,这个我还没来得及更改但不影响使用,我的模拟器有点问题所以程序没有安装成功,我在我的真机上已测试OK!