总结

1 创建桌面快捷方式

	//启动代码安装快捷方式
	Intent intent = new Intent();
	intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
	intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "手机卫士");
	intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,
				BitmapFactory.decodeResource(getResources(), R.drawable.safe));

	//手机卫士开启的意图
	Intent homeIntent = new Intent();
	homeIntent.setAction("com.ilmare.mobilesafe.home");
	homeIntent.addCategory("android.intent.category.DEFAULT");
	intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, homeIntent);
	sendBroadcast(intent);
需要的权限:

        <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
要打开的activity在清单文件中的配置

       <activity android:name="com.ilmare.mobilesafe.HomeActivity" >
            <intent-filter>
                <action android:name="com.ilmare.mobilesafe.home" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

2 拷贝资产(asset)目录下的数据库

<pre name="code" class="html">          private void copyDB(String dbfilename) {
		File file = new File(getFilesDir(), dbfilename);
		if (file.exists() && file.length() > 0) {
			Log.i(TAG, "数据库文件已经拷贝过了,无需重复拷贝");
		} else {
			try {
				// 数据库文件只需要拷贝一次,如果已经拷贝成功了。以后就不需要重复的拷贝了
				AssetManager am = getAssets();
				InputStream is = am.open(dbfilename);
				// 创建一个文件 /data/data/包名/files/address.db
				FileOutputStream fos = new FileOutputStream(file);
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = is.read(buffer)) != -1) {
					fos.write(buffer, 0, len);
				}
				is.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


 
getFilesDir() //获取内部存储文件的位置   data/data/packageName/files/.
<pre name="code" class="html">
 
<pre name="code" class="html"><pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Consolas';font-size:12.0pt;">getCacheDir() //获取内部缓存文件的位置   data/data/packageName/caches/.   
 
 3 Afinal  
下载 

if (Environment.getExternalStorageState().equals(
      Environment.MEDIA_MOUNTED)) {
   // 下载新的apk 路径 path
   FinalHttp fh = new FinalHttp();
   fh.download(path, Environment.getExternalStorageDirectory()
         .getAbsolutePath() + "/update.apk", new AjaxCallBack<File>() {

      @Override
      public void onFailure(Throwable t, int errorNo,
                       String strMsg) {
         Toast.makeText(getApplicationContext(),
               "更新失败", 0).show();
         enterHome();
         t.printStackTrace();
         super.onFailure(t, errorNo, strMsg);
      }

      @Override
      public void onLoading(long count, long current) {
         super.onLoading(count, current);
         int progress = (int) (current * 100 / count);
         tv_splash_updateinfo.setText("正在下载:"
               + progress + "%");
      }

      @Override
      public void onSuccess(File t) {
         super.onSuccess(t);
         installApk(t);
      }

      /**
       * 安装一个apk文件
       *
       * @param t
       */
      private void installApk(File t) {
         Intent intent = new Intent();
         intent.setAction("android.intent.action.VIEW");
         intent.addCategory("android.intent.category.DEFAULT");
         intent.setDataAndType(Uri.fromFile(t),
               "application/vnd.android.package-archive");
         startActivity(intent);
      }
   });


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值