android 在service 启动activity

我想我们一般在Service里想启动Activity一定会这样写: 

Java代码  收藏代码
  1. Intent intentv = new Intent(Intent.ACTION_VIEW);  
  2.             intentv.setData(uri);  
  3.             intentv.putExtra("keepTitle"true);  
  4.             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: 

Java代码  收藏代码
  1. TagService.saveMessages(this, msgs, false, getPendingIntent());private PendingIntent getPendingIntent() {  
  2.         Intent callback = new Intent();  
  3.         callback.setClass(this, TagViewer.class);  
  4.         callback.setAction(Intent.ACTION_VIEW);  
  5.         callback.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);  
  6.         callback.putExtra(EXTRA_KEEP_TITLE, true);  
  7.   
  8.         return PendingIntent.getActivity(this0, callback, PendingIntent.FLAG_CANCEL_CURRENT);  
  9.     }  



Java代码  收藏代码
  1. public static void saveMessages(Context context, NdefMessage[] msgs, boolean starred,  
  2.             PendingIntent pending) {  
  3.         Intent intent = new Intent(context, TagService.class);  
  4.         intent.putExtra(TagService.EXTRA_SAVE_MSGS, msgs);  
  5.         intent.putExtra(TagService.EXTRA_STARRED, starred);  
  6.         intent.putExtra(TagService.EXTRA_PENDING_INTENT, pending);  
  7.         context.startService(intent);  
  8.     }  
Java代码  收藏代码
  1. @Override  
  2.     public void onHandleIntent(Intent intent) {  
  3.         if (intent.hasExtra(EXTRA_SAVE_MSGS)) {  
  4.             Parcelable[] msgs = intent.getParcelableArrayExtra(EXTRA_SAVE_MSGS);  
  5.             NdefMessage msg = (NdefMessage) msgs[0];  
  6.   
  7.             ContentValues values = NdefMessages.toValues(this, msg, false, System.currentTimeMillis());  
  8.             Uri uri = getContentResolver().insert(NdefMessages.CONTENT_URI, values);  
  9.             if (intent.hasExtra(EXTRA_PENDING_INTENT)) {  
  10.                 Intent result = new Intent();  
  11.                 result.setData(uri);  
  12.   
  13.                 PendingIntent pending = (PendingIntent) intent.getParcelableExtra(EXTRA_PENDING_INTENT);  
  14.                
  15.                 try {  
  16.                     pending.send(this0, result);  
  17.                 } catch (CanceledException e) {  
  18.                     if (DEBUG) Log.d(TAG, "Pending intent was canceled.");  
  19.                 }  
  20.             }  
  21.   
  22.             return;  
  23.         }  
  24. .....  
  25. }  



通过pending.send(this, 0, result);启动了对应的Activity. 

这里也是PendingIntent的用法之一。 
我不太明白这两种启动Activity的方法有什么不同之处虽然效果都是一样的,对开销会怎样,希望知道的朋友能和我分享。呵呵..

原文地址:https://www.cnblogs.com/shenbin/p/3011374.html