开始下载在Android通知栏点击 [英] Start downloading on Notification bar click in android
问题描述
我的code显示通知栏,当我点击它开始下载。唯一的问题是它进入浏览器,并开始在那里。我想要下载开始,但它并没有去浏览器。我的code是:
意图NotifyIntent =新意图(android.content.Intent.ACTION_VIEW,Uri.parse(ApkFileLocation)); 的PendingIntent意图= PendingIntent.getActivity(EnquireActivity.this,0,NotifyIntent,0);
NotificationDetails.setLatestEventInfo(背景下,ContentTitle,ContentText,意向); mNotificationManager.notify(SIMPLE_NOTFICATION_ID,NotificationDetails);
通过设置 NotifyIntent
到 android.content.Intent.ACTION_VIEW
与URL,你是在告诉系统,我不在乎这是怎么打开的,所以使用默认的行为。
如果你想下载到发生在后台,你应该实施服务
,写code要下载的文件存在。然后让 NotifyIntent
指向一个广播接收器
这将启动服务
。服务应该能够显示为正在进行的活动的通知栏下载状态(不能被解雇)。
编辑:看起来你可以直接使用这种方式的意图启动服务:
意图NotifyIntent =新意图(背景下,MyService.class);
的PendingIntent意图= PendingIntent.getService(上下文,0,NotifyIntent,0);
My code shows notification bar and when I click on that it starts downloading. Only problem is it goes to the browser and starts there. I want that download starts but it doesn't go to browser. My code is :
Intent NotifyIntent = new Intent(android.content.Intent.ACTION_VIEW,Uri.parse(ApkFileLocation));
PendingIntent intent = PendingIntent.getActivity(EnquireActivity.this, 0, NotifyIntent, 0);
NotificationDetails.setLatestEventInfo(context, ContentTitle, ContentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID, NotificationDetails);
By setting NotifyIntent
to android.content.Intent.ACTION_VIEW
with a URL, you are telling the system, "I don't care how this is opened, so use the default behavior."
If you want the download to happen in the background you should implement a Service
and write code to download the file there. Then have NotifyIntent
point to a BroadcastReceiver
which will start the Service
. The service should be able to display the download status in the notification bar as an "ongoing" activity (can't be dismissed).
EDIT: It looks like you can start the Service directly using an Intent in this manner:
Intent NotifyIntent = new Intent(context, MyService.class);
PendingIntent intent = PendingIntent.getService(context, 0, NotifyIntent, 0);
这篇关于开始下载在Android通知栏点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!