- 浏览: 1058409 次
- 性别:
- 来自: 南昌
文章分类
- 全部博客 (276)
- 生活 (1)
- 代码之美 (22)
- Media (7)
- Android Widget (3)
- Android Intent (1)
- Android Activity (4)
- UI event handle--UI事件处理机制 (2)
- Java基础知识 (12)
- android Databases (5)
- Android 系统知识 (70)
- 平常遇到的问题与解决方法 (38)
- Android TextView/EditView (2)
- Thinking Java (1)
- android webkit (6)
- JSON (1)
- XML (4)
- HTTP (1)
- Google Weather API (1)
- android 2.3 NFC (10)
- android app (20)
- android framework (7)
- C++ (2)
- android System (5)
- Pthread (1)
- Wifi (8)
- Unix/Linux C (8)
- Android 4.0 (1)
- Mail (1)
- Smack 源码学习 (4)
- iOS (4)
- Android (1)
- git (1)
- Gallery3d (2)
- React-Natice (1)
最新评论
-
dd18349182956:
你是用的smack哪个版本?我用的smack4.1.3和sma ...
关于socket长连接的心跳包 -
xukaiyin:
全英文
getApplicationContext()与this,getBaseContext() -
裂风矢:
...
<category android:name="android.intent.category.DEFAULT" /> 惹的祸 -
xanthodont:
mark一下
XMPP——Smack -
Evilover3:
mark一下,学习了
XMPP——Smack
我想我们一般在Service里想启动Activity一定会这样写:
这样写就会报错的:
03-11 02:37:09.737: ERROR/AndroidRuntime(7881): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
提示要加上FLAG_ACTIVITY_NEW_TASK,所以加上 intentv.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);这一行就可以了。
但若看的源码够的话,还会发现有另外一种方法在Service里面启动Activity的另外一种方法:通过PendingIntent,下面就以Tag中TagView(Activity)和TagService为例子:
TagView里启动TagService并且把待会在TagService里面启动的Activity用Pending将其封装好并且传给TagService:
通过pending.send(this, 0, result);启动了对应的Activity.
这里也是PendingIntent的用法之一。
我不太明白这两种启动Activity的方法有什么不同之处虽然效果都是一样的,对开销会怎样,希望知道的朋友能和我分享。呵呵..
Intent intentv = new Intent(Intent.ACTION_VIEW); intentv.setData(uri); intentv.putExtra("keepTitle", true); startActivity(intentv);
这样写就会报错的:
03-11 02:37:09.737: ERROR/AndroidRuntime(7881): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
提示要加上FLAG_ACTIVITY_NEW_TASK,所以加上 intentv.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);这一行就可以了。
但若看的源码够的话,还会发现有另外一种方法在Service里面启动Activity的另外一种方法:通过PendingIntent,下面就以Tag中TagView(Activity)和TagService为例子:
TagView里启动TagService并且把待会在TagService里面启动的Activity用Pending将其封装好并且传给TagService:
TagService.saveMessages(this, msgs, false, getPendingIntent());private PendingIntent getPendingIntent() { Intent callback = new Intent(); callback.setClass(this, TagViewer.class); callback.setAction(Intent.ACTION_VIEW); callback.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP); callback.putExtra(EXTRA_KEEP_TITLE, true); return PendingIntent.getActivity(this, 0, callback, PendingIntent.FLAG_CANCEL_CURRENT); }
public static void saveMessages(Context context, NdefMessage[] msgs, boolean starred, PendingIntent pending) { Intent intent = new Intent(context, TagService.class); intent.putExtra(TagService.EXTRA_SAVE_MSGS, msgs); intent.putExtra(TagService.EXTRA_STARRED, starred); intent.putExtra(TagService.EXTRA_PENDING_INTENT, pending); context.startService(intent); }
@Override public void onHandleIntent(Intent intent) { if (intent.hasExtra(EXTRA_SAVE_MSGS)) { Parcelable[] msgs = intent.getParcelableArrayExtra(EXTRA_SAVE_MSGS); NdefMessage msg = (NdefMessage) msgs[0]; ContentValues values = NdefMessages.toValues(this, msg, false, System.currentTimeMillis()); Uri uri = getContentResolver().insert(NdefMessages.CONTENT_URI, values); if (intent.hasExtra(EXTRA_PENDING_INTENT)) { Intent result = new Intent(); result.setData(uri); PendingIntent pending = (PendingIntent) intent.getParcelableExtra(EXTRA_PENDING_INTENT); try { pending.send(this, 0, result); } catch (CanceledException e) { if (DEBUG) Log.d(TAG, "Pending intent was canceled."); } } return; } ..... }
通过pending.send(this, 0, result);启动了对应的Activity.
这里也是PendingIntent的用法之一。
我不太明白这两种启动Activity的方法有什么不同之处虽然效果都是一样的,对开销会怎样,希望知道的朋友能和我分享。呵呵..
发表评论
-
ACTIVITY的LAUNCH MODE详解 SINGLETASK正解
2012-05-30 08:58 1084转自:http://www.cnblogs.com/xiaoQ ... -
android的一些开源项目
2011-12-06 14:08 1164转自:http://www.iteye.com/problem ... -
修改StatusBar
2011-10-10 10:27 1237转自:http://iserveandroid.blogspo ... -
单独一个应用程序换语言
2011-09-29 15:16 1423转自http://blog.csdn.net/sodino/a ... -
通过代码设置live wall paper
2011-09-02 09:17 24921. The code is: private Wall ... -
Android camera 默认显示黑白的问题
2011-06-08 16:18 2490转自:http://tassardge.blog. ... -
关于Activity切换动画——overridePendingTransition
2011-05-25 14:33 5744Activity的切换动画指的是从一个activity跳转到另 ... -
sendBroadcast和sendStickyBroadcast的区别
2011-04-28 13:39 9674我们平时最经常使用的是sendBroadcast,就是把一个I ... -
Settings.System.getInt获取Setting里面的一些设置
2011-04-28 10:29 7004好久没有更新博客了,其实这期间我都在研究android如何换皮 ... -
Android Windows
2011-03-11 09:45 2027转自:http://elsila.blog.163.com/b ... -
IntentService
2011-03-11 09:23 3430看android的源码可以发现很多很多有趣有用的代码,今天在看 ... -
AsyncQueryHandler
2011-03-02 15:10 6771在QuickContactBadge里面我 ... -
QuickContactBadge如何实现
2011-03-02 11:52 4892从前一篇,我们知道了如何使用了QuikcContactBadg ... -
如何使用QuickContactBadge
2011-03-02 09:34 4022参考文章:http://mobile.tutsplus.com ... -
QuickSearcheBox---SearchManager获取search列表
2011-02-25 14:05 1845在android 2.2之后加入了SearchManager, ... -
QuickSearcheBox---SearchWidgetConfigActivity
2011-02-24 10:48 1611再把QuickSearchBox放到桌面前,会先触发它的Con ... -
RemoteView总结
2011-02-23 11:15 2304我最初认识RemoteView是在AppWidget里面的,但 ... -
Google Search Widget, Google Search ap, Globe Search ap
2011-02-23 10:39 1261http://blog.csdn.net/Judy889/ar ... -
调用android system Search UI须注意的问题
2011-02-23 10:38 1521http://blog.csdn.net/Judy889/ar ...
相关推荐
NULL 博文链接:https://410063005.iteye.com/blog/1941314
Service的启动,Activity与Service之间数据传递,Binder利用bindService实现Service传递数据给Activity,Activity的成员Broadcast实现Service传递数据给Activity
android,启动service,并且service把值传给Activity显示
android开机启动Activity/Service/App的三合一demo,本人写了一个小小demo,把如果开机自启动Activity、Service、APP写了下,欢迎下载。
启动后台service后,service内跑一个线程,5秒后发送一个广播给系统,Activity接收到该广播后显示service发送的信息。本例主要是实现后台service与前activity的交互
NULL 博文链接:https://hou1000y.iteye.com/blog/765235
安卓开发观察者模式应用-一个service通知多个activity更新ui的实现, service与activity的交互: 1.接口回调 2.自定义广播 3.观察者模式
android 后台启动service监听某个函数的状态是否发生改变,如果改变则 通过broadcast通知Myreceiver,启动另一个activity
Activity和Service是应用程序的核心组件,程序框架层提供了一套完整的机制来协助应用程序启动Activity和Service
两种方式启动service比较,activity与service通信
知识点:广播必须在activity或者service启动之后才能发挥作用。个人看来是因为开启activity或者service时, AndroidManifest.xml里面的注册的广播或者在Activity、service动态注册的广播接收器才能被开启,这样才能...
点击通知启动多个Activity.从启动Activity 返回不会退出app,而是返回到App 的MainActivity 或者其他Activity
在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activity中启动后台Service,通过Intent来启动,...
Now you can launch an explicit Activity like this: SmartGo.from(this) .to(ExplicitActivity.class) .shareElements() .like(v) .withSystemUI() .go(); or an implicit Activity like: SmartGo.from(this)...
在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activity中启动后台Service,通过Intent来启动,...
如果我们添加的音乐播放器的这个...它的功能类似于Activity,只是Service是在后台工作,不出现在用户的视野中。当我们在前台上进行一个人机交互操作,让这个操作关联到Service上,这样,我们就能够解决这个问题了。
本示例 利用Broadcast Receiver 启动Service,在这里同时又启动了一个Activity用于提示服务启动成功
一个小demo,通过求平均值,测试service的生命周期以及启动方式!
Android Activity、Service、Broadcast、Notification四合一例子,Activity中点击按钮启动服务,服务里每4秒发送一次广播,广播接受者每接受到一个发送一条通知。