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;
}

}

更多相关文章

  1. 【android】简单的根据url下载图片的一个类
  2. android中利用pull解析xml文件
  3. Android(安卓)桌面快捷方式操作
  4. android API Level 19 No system images installed for this tar
  5. android 数据库更改数据库位置【DbFlow示范】
  6. 移植unrar到Android
  7. android proguard 错误处理
  8. Android(安卓)各种Context区别
  9. Android(安卓)MD5加密

随机推荐

  1. .NET跨平台开发之Xamarin.Android介绍与
  2. windows 下 android 使用ant自动打包
  3. Android总结篇系列:Android广播机制----学
  4. Android JNI中记录log
  5. Android:删除预装应用
  6. android 决TextView中MaxLines与ellipsiz
  7. 解决国内android sdk无法更新,google不能
  8. A010-menu资源
  9. android 多语言的实现
  10. 【Android】数据存储之Shared Preference