android 创建快捷方式 删除快捷方式 不重复判断快捷方式

package com.cn.yingmob.service;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.cn.yingmob.webview.qqWebview;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

@SuppressLint("NewApi")
public class quickService extends Service {
 private Bitmap bitmap;
 private Bitmap bitmap1;
 private String fileName;
 private String fileName1;
 private final static String ALBUM_PATH = Environment
   .getExternalStorageDirectory() + "/download_quick/";

 @Override
 public void onCreate() {
  // TODO Auto-generated method stub
  super.onCreate();
  new Thread(connectNet).start();
  Log.d("TAG", "dddddddd");
 }

 private Runnable connectNet = new Runnable() {
  @SuppressLint("ShowToast")
  @Override
  public void run() {
   try {
    String filePath = "http://img0.pconline.com.cn/pconline/1112/21/2626596_qq.jpg";
    fileName = "qq.jpg";
    String filePath1 = "http://a2.att.hudong.com/10/96/300000931099127952960461732.jpg";
    fileName1 = "baidu.jpg";
    Log.d("TAG", "ssssssss");
    // 取得的是byte数组, 从byte数组生成bitmap
    byte[] data = getImage(filePath);
    if (data != null) {
     bitmap = BitmapFactory
       .decodeByteArray(data, 0, data.length);// bitmap
     saveFile(bitmap, fileName);
     Log.d("TAG", "wwwww");
    } else {
     Toast.makeText(quickService.this, "Image error!", 1).show();
    }
    bitmap1 = BitmapFactory.decodeStream(getImageStream(filePath1));
    saveFile(bitmap1, fileName1);
    Log.d("TAG", "set image ...");
   } catch (Exception e) {
    Toast.makeText(quickService.this, "无法链接网络!", 1).show();
    e.printStackTrace();
   }

  }

 };

 public InputStream getImageStream(String path) throws Exception {
  URL url = new URL(path);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setConnectTimeout(5 * 1000);
  conn.setRequestMethod("GET");
  if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
   return conn.getInputStream();
  }
  return null;
 }

 public byte[] getImage(String path) throws Exception {
  URL url = new URL(path);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setConnectTimeout(5 * 1000);
  conn.setRequestMethod("GET");
  InputStream inStream = conn.getInputStream();
  if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
   return readStream(inStream);
  }
  return null;
 }

 public static byte[] readStream(InputStream inStream) throws Exception {
  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = inStream.read(buffer)) != -1) {
   outStream.write(buffer, 0, len);
  }
  outStream.close();
  inStream.close();
  return outStream.toByteArray();
 }

 /**
  * 保存文件
  *
  * @param bm
  * @param fileName
  * @throws IOException
  */
 public void saveFile(Bitmap bm, String fileName) throws IOException {
  File dirFile = new File(ALBUM_PATH);
  if (!dirFile.exists()) {
   dirFile.mkdir();
  }
  File myCaptureFile = new File(ALBUM_PATH + fileName);
  BufferedOutputStream bos = new BufferedOutputStream(
    new FileOutputStream(myCaptureFile));
  bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
  String srcString = myCaptureFile.toString();
  Log.d("TAG", srcString);
  bos.flush();
  bos.close();

 }

 /**
  * 为程序创建桌面快捷方式
  */
 private void addShortcut() {
  Intent shortcut = new Intent(
    "com.android.launcher.action.INSTALL_SHORTCUT");
  // 快捷方式的名称
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "腾讯");
  shortcut.putExtra("duplicate", true); // 不允许重复创建
  Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
  shortcutIntent.setClassName(this, this.getClass().getName());
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
  String secString = bitmap + fileName;
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inSampleSize = 2;
  Bitmap bm = BitmapFactory.decodeFile(secString, options);
  shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, bm);
  Log.d("TAG", secString);
  // 点击快捷图片,运行的程序主入口
  shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
    getApplicationContext(), qqWebview.class));
  sendBroadcast(shortcut);
 }

 /**
  * 判断是否已经存在桌面图标
  *
  * @param context
  * @return
  */
 private boolean hasShortcut(String appName) {
  boolean Shortcut = false;
  String url = "content://com.android.launcher2.settings/favorites?notify=true";
  ContentResolver resolver = getContentResolver();
  Cursor cursor = resolver.query(Uri.parse(url), null, "title=?",
    new String[] { appName }, null);
  if (cursor != null && cursor.getCount() > 0) {
   return Shortcut = true;
  }
  return Shortcut;
 }

 /**
  * 删除快捷方式
  */
 private void deleteShortcut(String appName) {
  Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
  shortcutIntent.setClassName(this, this.getClass().getName());
  Intent intent = new Intent(
    "com.android.launcher.action.UNINSTALL_SHORTCUT");
  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
  sendBroadcast(intent);
 }

 @SuppressWarnings("deprecation")
 @Override
 public void onStart(Intent intent, int startId) {
  // TODO Auto-generated method stub
  super.onStart(intent, startId);
  if (hasShortcut("腾讯")) {
   deleteShortcut("腾讯");
   addShortcut();
  } else {
   addShortcut();
  }

 }

 @Override
 public void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  // deleteShortcut("腾讯");
  Log.i("TAG", "已经结束服务!");
 }

 @Override
 public IBinder onBind(Intent arg0) {
  // TODO Auto-generated method stub
  return null;
 }

}

转载于:https://my.oschina.net/u/1993567/blog/297233

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值