Skip to content

Commit

Permalink
app在线更新
Browse files Browse the repository at this point in the history
  • Loading branch information
teprinciple committed Dec 29, 2016
0 parents commit 4b0fe3a
Show file tree
Hide file tree
Showing 33 changed files with 915 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
.externalNativeBuild
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
29 changes: 29 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.teprinciple.updateappdemo"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\Teprinciple\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.teprinciple.updateappdemo;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.example.teprinciple.updateappdemo", appContext.getPackageName());
}
}
34 changes: 34 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.teprinciple.updateappdemo">

<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<receiver
android:name=".updateapp.UpdateAppReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>
<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.teprinciple.updateappdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.teprinciple.updateappdemo.updateapp.UpdateAppUtil;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void updateApp(View view) {
UpdateAppUtil.updateApp(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.example.teprinciple.updateappdemo.customview;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.example.teprinciple.updateappdemo.R;
import com.example.teprinciple.updateappdemo.customview.feature.Callback;


/**
* Created by Teprinciple on 2016/10/13.
*/
public class ConfirmDialog extends Dialog implements View.OnClickListener {

Callback callback;
private TextView content;
private TextView sureBtn;
private TextView cancleBtn;

public ConfirmDialog(Context context, Callback callback) {
super(context, R.style.CustomDialog);
this.callback = callback;
setCustomDialog();
}

private void setCustomDialog() {
View mView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_confirm, null);
sureBtn = (TextView)mView.findViewById(R.id.dialog_confirm_sure);
cancleBtn = (TextView)mView.findViewById(R.id.dialog_confirm_cancle);
content = (TextView) mView.findViewById(R.id.dialog_confirm_title);
sureBtn.setOnClickListener(this);
cancleBtn.setOnClickListener(this);
super.setContentView(mView);
}


public ConfirmDialog setContent(String s){
content.setText(s);
return this;
}


@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.dialog_confirm_cancle:
this.cancel();
break;

case R.id.dialog_confirm_sure:
callback.callback();
this.cancel();
break;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.teprinciple.updateappdemo.customview.feature;

/**
* Created by sanmu on 2016/10/13 0013.
*/
public interface Callback {
public void callback();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.example.teprinciple.updateappdemo.updateapp;

import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import java.io.File;



/**
*Created by Teprinciple on 2016/12/13.
*/
public class DownloadAppUtils {
private static final String TAG = DownloadAppUtils.class.getSimpleName();
public static long downloadUpdateApkId = -1;//下载更新Apk 下载任务对应的Id
public static String downloadUpdateApkFilePath;//下载更新Apk 文件路径

/**
* 通过浏览器下载APK包
* @param context
* @param url
*/
public static void downloadForWebView(Context context, String url) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "tmp.apk")),
"application/vnd.android.package-archive");
context.startActivity(intent);
}


/**
* 下载更新apk包
* 权限:1,<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
* @param context
* @param url
*/
public static void downloadForAutoInstall(Context context, String url, String fileName, String title) {
//LogUtil.e("App 下载 url="+url+",fileName="+fileName+",title="+title);
if (TextUtils.isEmpty(url)) {
return;
}
try {
Uri uri = Uri.parse(url);
DownloadManager downloadManager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
//在通知栏中显示
request.setVisibleInDownloadsUi(true);
request.setTitle(title);
String filePath = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//外部存储卡
filePath = Environment.getExternalStorageDirectory().getAbsolutePath();

} else {
Log.i(TAG,"没有SD卡");
return;
}
downloadUpdateApkFilePath = filePath + File.separator + fileName;
// 若存在,则删除
deleteFile(downloadUpdateApkFilePath);
Uri fileUri = Uri.parse("file://" + downloadUpdateApkFilePath);
request.setDestinationUri(fileUri);
downloadUpdateApkId = downloadManager.enqueue(request);
} catch (Exception e) {
e.printStackTrace();
downloadForWebView(context, url);
}finally {
// registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}


private static boolean deleteFile(String fileStr) {
File file = new File(fileStr);
return file.delete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.example.teprinciple.updateappdemo.updateapp;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;


/**
* 注册
* <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
* <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
*/
public class UpdateAppReceiver extends BroadcastReceiver {
public UpdateAppReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {

// 处理下载完成
Cursor c=null;
try {
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
if (DownloadAppUtils.downloadUpdateApkId >= 0) {
long downloadId = DownloadAppUtils.downloadUpdateApkId;
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
DownloadManager downloadManager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
c = downloadManager.query(query);
if (c.moveToFirst()) {
int status = c.getInt(c
.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status == DownloadManager.STATUS_FAILED) {
downloadManager.remove(downloadId);

} else if (status == DownloadManager.STATUS_SUCCESSFUL) {
if (DownloadAppUtils.downloadUpdateApkFilePath != null) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(
Uri.parse("file://"
+ DownloadAppUtils.downloadUpdateApkFilePath),
"application/vnd.android.package-archive");
//todo 针对不同的手机 以及sdk版本 这里的uri地址可能有所不同
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
}
}/* else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) {//点击通知取消下载
DownloadManager downloadManager = (DownloadManager) context
.getSystemService(Context.DOWNLOAD_SERVICE);
long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
//点击通知栏取消下载
downloadManager.remove(ids);
}*/

} catch (Exception e) {
e.printStackTrace();
}finally {
if (c != null) {
c.close();
}
}
}
}
Loading

0 comments on commit 4b0fe3a

Please sign in to comment.