80,362
社区成员
发帖
与我相关
我的任务
分享
public void sendNotification(View view) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(android.R.drawable.sym_action_call);
mBuilder.setContentTitle(title.getText().toString());
mBuilder.setContentText(content.getText().toString());
Intent intent = new Intent(this, NotificationActivity.class);
// 传值
Bundle bundle = new Bundle();
bundle.putString("title", title.getText().toString());
bundle.putString("content", content.getText().toString());
intent.putExtras(bundle);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NotificationActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// 按下通知栏按钮之后跳转页面
mBuilder.setContentIntent(resultPendingIntent);
// 通知管理器
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 利用notify方法发送通知
nm.notify(1, mBuilder.build());
}
/***
* 开线程下载
*/
public void createThread() {
/***
* 更新UI
*/
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_OK:
// 下载完成,点击安装
Uri uri = Uri.fromFile(newVersion);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,"application/vnd.android.package-archive");
pendingIntent = PendingIntent.getActivity(UpdateService.this, 0, intent, 0);
notification.icon = R.drawable.ic_launcher;
contentView.setImageViewResource(R.id.notificationImage, R.drawable.beicai_nopreesed);
notification.setLatestEventInfo(UpdateService.this,appName, "下载成功,点击安装", pendingIntent);
notificationManager.notify(notification_id, notification);
stopSelf();
break;
case DOWN_ERROR:
notification.setLatestEventInfo(UpdateService.this,appName, "下载失败", pendingIntent);
break;
default:
stopSelf();
break;
}
}
};
final Message message = new Message();
new Thread(new Runnable() {
@Override
public void run() {
try {
long downloadSize = downloadUpdateFile(down_url,
newVersion.toString());
if (downloadSize > 0) {
// 下载成功
message.what = DOWN_OK;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
message.what = DOWN_ERROR;
handler.sendMessage(message);
}
}
}).start();
}
/***
* 创建通知栏
*/
RemoteViews contentView;
public void createNotification() {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.icon = android.R.drawable.stat_sys_download;
// // 这个参数是通知提示闪出来的值.
// notification.tickerText = "开始下载";
//
// updateIntent = new Intent(this, MainActivity.class);
// pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);
//
// // 这里面的参数是通知栏view显示的内容
// notification.setLatestEventInfo(this, app_name, "下载:0%",
// pendingIntent);
//
// notificationManager.notify(notification_id, notification);
/***
* 在这里我们用自定的view来显示Notification
*/
contentView = new RemoteViews(getPackageName(),R.layout.notification_item);
contentView.setTextViewText(R.id.notificationTitle, "正在下载");
contentView.setTextViewText(R.id.notificationPercent, "0%");
contentView.setProgressBar(R.id.notificationProgress, 100, 0, false);
notification.contentView = contentView;
/*updateIntent = new Intent(this, MainActivity.class);
updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);
notification.contentIntent = pendingIntent;*/
notificationManager.notify(notification_id, notification);
}
/***
* 下载文件
*
* @return
* @throws MalformedURLException
*/
public long downloadUpdateFile(String down_url, String file)
throws Exception {
int down_step = 5;// 提示step
int totalSize;// 文件总大小
int downloadCount = 0;// 已经下载好的大小
int updateCount = 0;// 已经上传的文件大小
InputStream inputStream;
OutputStream outputStream;
URL url = new URL(down_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(TIMEOUT);
httpURLConnection.setReadTimeout(TIMEOUT);
// 获取下载文件的size
totalSize = httpURLConnection.getContentLength();
if (httpURLConnection.getResponseCode() == 404) {
throw new Exception("fail!");
}
inputStream = httpURLConnection.getInputStream();
outputStream = new FileOutputStream(file, false);// 文件存在则覆盖掉
byte buffer[] = new byte[1024];
int readsize = 0;
while ((readsize = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, readsize);
downloadCount += readsize;// 时时获取下载到的大小
/**
* 每次增张5%
*/
if (updateCount == 0
|| (downloadCount * 100 / totalSize - down_step) >= updateCount) {
updateCount += down_step;
// 改变通知栏
// notification.setLatestEventInfo(this, "正在下载...", updateCount
// + "%" + "", pendingIntent);
contentView.setTextViewText(R.id.notificationPercent,
updateCount + "%");
contentView.setProgressBar(R.id.notificationProgress, 100,
updateCount, false);
// show_view
notificationManager.notify(notification_id, notification);
}
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
inputStream.close();
outputStream.close();
return downloadCount;
}
public class OpenFiles {
//android获取一个用于打开HTML文件的intent
public static Intent getHtmlFileIntent(File file)
{
Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(uri, "text/html");
return intent;
}
//android获取一个用于打开图片文件的intent
public static Intent getImageFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "image/*");
return intent;
}
//android获取一个用于打开PDF文件的intent
public static Intent getPdfFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
return intent;
}
//android获取一个用于打开文本文件的intent
public static Intent getTextFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "text/plain");
return intent;
}
//android获取一个用于打开音频文件的intent
public static Intent getAudioFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "audio/*");
return intent;
}
//android获取一个用于打开视频文件的intent
public static Intent getVideoFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("oneshot", 0);
intent.putExtra("configchange", 0);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "video/*");
return intent;
}
//android获取一个用于打开CHM文件的intent
public static Intent getChmFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/x-chm");
return intent;
}
//android获取一个用于打开Word文件的intent
public static Intent getWordFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/msword");
return intent;
}
//android获取一个用于打开Excel文件的intent
public static Intent getExcelFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.ms-excel");
return intent;
}
//android获取一个用于打开PPT文件的intent
public static Intent getPPTFileIntent(File file)
{
Intent intent = new Intent("android.intent.action.VIEW");
intent.addCategory("android.intent.category.DEFAULT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
return intent;
}
//android获取一个用于打开apk文件的intent
public static Intent getApkFileIntent(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
return intent;
}
}
下载完成之后 先从里面获取你需要的打开的文件类型的intent 更新一下 通知栏就ok了 就可以点击打开那个文件了
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this,appName, "下载成功,点击安装", pendingIntent);
notificationManager.notify(notification_id, notification);