某项工作需要要写一个下载文件的管理器,我当时就想到了market上的下载,觉得不错,于是开始了我的搜寻和研究,不断地google啊,翻墙啊,终于找到了一个叫downloadProvider的东东,看名字就知道是个Provider是用来提供给其他应用程序用的,猜想market应该也是直接用的它吧;研究了一下发现用起来很简单,可是总是抛出权限异常,于是我加上了android.permission.ACCESS_DOWNLOAD_MANAGER_ADVANCED ,可是结果依然有权限问题,只好继续查资料,
于是看到”不幸的是,DownloadProvider目前在非系统的应用程序中不可用,而且在可预见的未来这一情况也不会改变,而且更加不幸的是,当前的DownloadProvier使用了私有的APIs以阻止SDK上的重编译,也就是说你不能使用它在你自己的应用程序里,高质量的贡献还是易于接受的如果各种影响被提前同步。
应用了DownloadProvider的系统的应用程序有Browser,Gmail,Market,和System Updater.”
不甘心,想找找看有没有解决的方法,终于看到了这个帖子-------http://blog.csdn.net/lixinso/archive/2010/09/16/5889159.aspx
可是这样的方法只能是自己的一个小程序自己放到自己手机上用,因为你不可能让所有使用你的application的用户去修改root然后push进apk文件,同样别人的downloadProvider你也不能全部修改,(这只是个人理解加别人讲解,嘿嘿,要是真的能实现当然也很好),这样就不好办了,就不想用它了(我自己是这样想的)
后来就去研究了downloadProvider的源代码,发现要实现类似的功能并不是很难,就是线程、notification等的使用,至于broadcast、Provider什么的做自己的应用可以不用的;自己写也很快的,当然我的代码和源代码是不能比的,不过大概的功能差不多大家可以研究后自己改进,也可以自己研究去~~
下面贴代码了:
主要就是使用notification,它的使用可以不再主ui里面,使用也很简单
主要功能是点击第一个Button累计添加notification
点击第四个Button实现不同状态的改变
中间两个没有事件响应
里面有很多参数的应用我没有一一实验,比如notification的flag参数等
这个是Activity按上面的按钮出现提示,layout文件很简单,略过
package com.clov4r.android.rmtv;
import com.clov4r.android.rmtv.data.ReadData;
import com.clov4r.android.rmtv.download.DatabaseHelper;
import com.clov4r.android.rmtv.download.DownloadThread;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class TestActivity extends Activity {
NotificationManager notificationManager = null;
static DatabaseHelper mDatabaseHelper = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
notificationManager = (NotificationManager) this
.getSystemService(NOTIFICATION_SERVICE);
Button btnshow = (Button) findViewById(R.id.Button01);
btnshow.setOnClickListener(listener);
Button btnchange = (Button) findViewById(R.id.Button03);
btnchange.setOnClickListener(listener);
Button btnrechange = (Button) findViewById(R.id.Button02);
btnrechange.setOnClickListener(listener);
Button btncancle = (Button) findViewById(R.id.Button04);
btncancle.setOnClickListener(listener);
}
int t = 1;
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.Button01:
new Thread(){
public void run(){
showNotification(null,t);
}
}.start();
t++;
// Notification n = new Notification();
// n.icon = android.R.drawable.stat_sys_download;
// n.tickerText = "download start";
// n.when = System.currentTimeMillis();
// n.defaults = Notification.DEFAULT_SOUND;
// n.flags = Notification.FLAG_AUTO_CANCEL;
// notificationManager.notify(1, n);
break;
case R.id.Button03:
break;
case R.id.Button02:
break;
case R.id.Button04:
insertItem("huanxi101116","womanmagazige","uri");
break;
}
}
};
public void insertItem(String title,String info,String uri) {
new DownloadThread(this,1,"title","uri",ReadData.getPath(this),"info").start();
}
//这个是验证了一下,是不是根据id就可以找到和修改一个notification,结果是yes,如果使用同一个对象也要使用相同的id,至于对象相同id不同是否是同一个还没有验证,理论上应该不是的吧,我主要是认为只和id有关,待会验证看看...
private void showNotification(Message msg, int id) {
NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.stat_sys_download, "开始下载", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
// Build the RemoteView object
RemoteViews expandedView = new RemoteViews(
this.getPackageName(),
R.layout.status_bar_ongoing_event_progress_bar);
expandedView.setTextViewText(R.id.description,
"item.description");
expandedView.setTextViewText(R.id.title, "title");
expandedView.setProgressBar(R.id.progress_bar,
100,
20,
true);
expandedView.setTextViewText(R.id.progress_text,"25%");
expandedView.setImageViewResource(R.id.appIcon,
android.R.drawable.stat_sys_download);
notification.contentView = expandedView;
Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("info", "msginfo");
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);
// notification.setLatestEventInfo(this, "下载info", "下载中...",contentIntent);
notification.contentIntent = contentIntent;//点击打开的Activity
notiManager.notify(id, notification);
}
}
下面是downloadthread
package com.clov4r.android.rmtv.download;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.widget.RemoteViews;
import com.clov4r.android.rmtv.MainActivity;
import com.clov4r.android.rmtv.R;
public class DownloadThread extends Thread {
private int download_dataid ;
private String download_datatitle ;
private String download_datauri ;
private String download_path;
private String download_info;
Context context;
public DownloadThread(Context c,int id,String title,String uri,String path,String info){
context = c;
download_dataid = id;
download_datatitle = title;
download_datauri = uri;
download_path = path;
download_info = info;
}
public void run(){
if(download_datauri==null||download_datauri.equals("")){
//broadcast "uri is invalid == null and void"
return;
}
//开始下载,修改提示状态,点击无效
activeNotification(download_dataid);
//下载ing ,修改状态,点击无效
//执行各种操作,读取数据,图片,下载文件等
try {
this.currentThread().sleep(2000);
complementNotification(download_dataid);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//下载完成,修改状态,点击有效
//用户点击有效
}
NotificationManager notiManager = null;
Notification notification = null;
private void activeNotification(int id) {
if(notiManager==null)
notiManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notification = new Notification(android.R.drawable.stat_sys_download, "开始下载", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
// Build the RemoteView object
RemoteViews expandedView = new RemoteViews(
context.getPackageName(),
R.layout.status_bar_ongoing_event_progress_bar);
expandedView.setTextViewText(R.id.description,download_info);
expandedView.setTextViewText(R.id.title, download_datatitle);
expandedView.setProgressBar(R.id.progress_bar,
100,
20,
true);
expandedView.setTextViewText(R.id.progress_text,"25%");
expandedView.setImageViewResource(R.id.appIcon,
android.R.drawable.stat_sys_download);
notification.contentView = expandedView;
//打开查看下载进度Activity
Intent intent = new Intent(context, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("info", "msginfo");
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);
// notification.setLatestEventInfo(this, "下载info", "下载中...",contentIntent);
notification.contentIntent = contentIntent;//点击打开的Activity
notiManager.notify(id, notification);
}
//完成下载
private void complementNotification(int id){
// Build the RemoteView object
notification = new Notification(android.R.drawable.stat_sys_download_done, "下载完成", System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
RemoteViews expandedView = new RemoteViews(
context.getPackageName(),
R.layout.status_bar_finish_event_progress_bar);
expandedView.setTextViewText(R.id.description,"下载完成,点击播放");
expandedView.setTextViewText(R.id.title, download_datatitle);
expandedView.setTextViewText(R.id.progress_text,"100%");
expandedView.setImageViewResource(R.id.appIcon,
android.R.drawable.stat_sys_download_done);
notification.contentView = expandedView;
//打开播放Activity
Intent intent = new Intent(context, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("info", "msginfo");
intent.putExtras(bundle);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, id,intent, PendingIntent.FLAG_UPDATE_CURRENT);
// notification.setLatestEventInfo(this, "下载info", "下载中...",contentIntent);
notification.contentIntent = contentIntent;//点击打开的Activity
notiManager.notify(id, notification);
}
}
status_bar_finish_event_progress_bar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:drawable/status_bar_item_app_background"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="40dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingTop="8dp"
android:focusable="true"
android:clickable="true"
>
<com.android.server.status.AnimatedImageView
android:id="@+id/appIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@android:drawable/sym_def_app_icon"
/>
<TextView android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff000000"
android:singleLine="true"
android:textSize="14sp"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:focusable="true" android:clickable="true" android:layout_alignParentTop="true" android:paddingTop="10dp" android:layout_height="fill_parent" android:gravity="center_vertical">
<TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:textColor="#ff000000" android:paddingLeft="2dp" android:textSize="20sp"/>
<TextView android:id="@+id/description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ff000000" android:singleLine="true" android:paddingLeft="5dp" android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
<com.android.server.status.AnimatedImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@android:drawable/divider_horizontal_bright"
/>
</LinearLayout>