Android_notification的应用

来源:互联网 发布:章鱼软件 编辑:程序博客网 时间:2024/06/05 06:40

在网上找的代码,注释比较好,代码也写得很规范,就是找不到作者,这里先感谢他。


直接看源码:

notificationActivity

package com.example.notificationdemo;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.app.AlertDialog;import android.app.Notification;import android.app.NotificationManager;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.RemoteViews;import android.widget.TextView;import android.widget.Toast;/** * @author devilmaycry * 改Demo的主要功能为下载应用,并在通知栏显示其进度,下载完成后会有一个对话框提示用户是否安装,如果不需要可以删除 * 建议看代码的童鞋,用eclipse看,并且双击类标签全屏看(因为写代码的时候是全屏下写的,写完后也没有对代码进行format,注释和代码都是一句到底不换行); */public class MainActivity extends Activity {private int progress; // 定义进度值int handmsg = 1;//private NotificationManager nm = null;private Notification nn = null; // 引入通知private RemoteViews view = null; // 用来设置通知的Viewprivate String apkDownloadPath; // 应用下载的地址private String savePath; // APK下载之后保存的地址private String saveFileName; // APK的文件名private static final int DOWN_UPDATE = 0;// 下载中消息private static final int DOWN_OVER = 1;// 下载完成消息private TextView marquee;//跑马灯文本框private Button start;//开始按钮private Button install;//安装;主要是用来当第一次下载完成提示安装时,用户选择错误,到时对话框消失,点击此按钮可以重新弹出对话框private AlertDialog dlg = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取系统通知的服务nn = new Notification();//创建一个通知对象marquee = (TextView) findViewById(R.id.marquee);//获取跑马灯文本控件marquee.setText("请注意:这里的应用是在应用商店里随便找的一个应用,如果怕不安全可以手动更改apkDownloadPath的值,来下载你想要下载的APK");//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////这是百度应用商店上的一个叫魔幻古筝的APK安装包,有其他需要可以自己找,是个安装包的路径就行apkDownloadPath = "http://gdown.baidu.com/data/wisegame/a5947fef7e036da5/MagicZither_7.apk";// 存放位置为手机默认目录下的NotificationDemo文件夹(如果没有会默认生成一个这样的文件夹,详见下载块)savePath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/NotificationDemo";// 为了测试我们把下载的apk的文件名也明明为NotificationDemosaveFileName = savePath + "/NotificationDemo.apk";////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////install = (Button) findViewById(R.id.install);//获取安装按钮控件start = (Button) findViewById(R.id.start_all);//获取启动按钮控件//启动按钮的监听start.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {install.setVisibility(View.GONE);ShowToast("开始在后台下载新版本", MainActivity.this);view = new RemoteViews(getPackageName(), R.layout.download_progress_state_view);nn.icon = R.drawable.ic_launcher;view.setImageViewResource(R.id.download_progress_img, R.drawable.ic_launcher);//new Thread(mdownApkRunnable).start();//如需使用外部浏览器下载,注释掉上边的线程,解开此句即可downloadByBrowser(apkDownloadPath);}});install.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {dlg.show();}});}// 下载APK的线程匿名类STARTprivate Runnable mdownApkRunnable = new Runnable() {@Overridepublic void run() {try {URL url = new URL(apkDownloadPath);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.connect();int length = conn.getContentLength();InputStream is = conn.getInputStream();File file = new File(savePath);Log.e("test", file.exists()+"");if (!file.exists()) {Log.e("test1", file.exists()+"");file.mkdir();Log.e("test2", file.exists()+"");}String apkFile = saveFileName;Log.e("test3", apkFile);File ApkFile = new File(apkFile);FileOutputStream fos = new FileOutputStream(ApkFile);int count = 0;byte buf[] = new byte[1024];do {int numread = is.read(buf);count += numread;progress = (int) (((float) count / length) * 100);if(handmsg < progress){handmsg ++;mHandler.sendEmptyMessage(DOWN_UPDATE);}// 更新进度if (numread <= 0) {// 下载完成通知安装mHandler.sendEmptyMessage(DOWN_OVER);break;}fos.write(buf, 0, numread);} while (true);// 点击取消就停止下载.fos.close();is.close();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();Log.e("test", e.getMessage());}}};// 下载APK的线程匿名类END// 处理下载进度的Handler Startprivate Handler mHandler = new Handler() {public void handleMessage(Message msg) {switch (msg.what) {case DOWN_UPDATE:ShowToast(progress, MainActivity.this);view.setProgressBar(R.id.download_progressbar, 100, handmsg,false);view.setTextViewText(R.id.download_progress_text, handmsg + "%");//设置notification的显示Viewnn.contentView = view;//通知显示notificationnm.notify(0, nn);super.handleMessage(msg);break;case DOWN_OVER:install.setVisibility(View.VISIBLE);ShowToast("下载完成",MainActivity.this);dlg = new AlertDialog.Builder(MainActivity.this).setTitle("安装").setMessage("下载完成是否安装").setPositiveButton("安装",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {installApk();install.setVisibility(View.GONE);}}).setNegativeButton("删除",new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {if(deleteAllFilesOfDir(new File(savePath))){ShowToast("存放目录和APK已删除", MainActivity.this);}else{ShowToast("删除失败,请检查路径,并手动删除", MainActivity.this);}install.setVisibility(View.GONE);}}).setNeutralButton("取消", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {dlg.dismiss();}}).show();break;default:break;}};};// 处理下载进度的Handler End//Toast方法(实在是懒得写一次Toast就写一次make写一次show。有时候show还忘了。。就这么干了,成学员要学着变懒才有优化代码的动力)private static void ShowToast(Object msg,Context context){Toast.makeText(context, msg+"", Toast.LENGTH_SHORT).show();}// 安装apkprivate void installApk() {File apkfile = new File(saveFileName);if (!apkfile.exists()) {ShowToast("要安装的文件不存在,请检查路径", MainActivity.this);return;}Intent i = new Intent(Intent.ACTION_VIEW);i.setDataAndType(Uri.parse("file://" + apkfile.toString()),"application/vnd.android.package-archive");startActivity(i);}// 删除APK/** * @param path  Apk存放的目录,是目录,不是APK文件的路径!否则只会删除APK 不会删除存放的目录 * @return  */public static boolean deleteAllFilesOfDir(File path) {      if (!path.exists())          return false;      if (path.isFile()) {          path.delete();          return true;      }      File[] files = path.listFiles();      for (int i = 0; i < files.length; i++) {          deleteAllFilesOfDir(files[i]);      }      path.delete();      return true;}  ///////////////////////////////////////////////////////////////////////////////////////////////////////////其实下载应用还有一个更简单的方法,只不过需要借助其他的浏览器应用,没法跟踪进度,Notification也是浏览器自带的,不过如果是需要做版本//更新的童鞋倒是推荐用下面的方法,因为不要自己动手,不占自己应用的资源,Notification是浏览器自带的,可以显示下载进度,比较的简便/////////////////////////////////////////////////////////////////////////////////////////////////////////private void downloadByBrowser(String apkDownloadPath){Uri uri  = Uri.parse(apkDownloadPath);Intent intent = new Intent(Intent.ACTION_VIEW,uri);startActivity(intent);}//就这样就行了,剩下的就交给外部的浏览器就行了。。。。。。。}

两个布局文件很简单,这里就不贴出来了。

0 0