您的位置:首页 > 移动开发 > Android开发

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

2014-08-01 00:00 531 查看
摘要: 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
7fe0
");
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息