`
huakewoniu
  • 浏览: 46458 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

android 开发中的一些小知识点

阅读更多

1 关于onPause():

onPause() is always called when the Activity ends, even if we
instigated that (with a finish() call for example). We will use this to save the current data back to the database.  Good practice is to release any resources that can be released during an onPause() as well, to take up less resources when in the passive state。

2 关于onSaveInstanceState();

There is no guarantee that onSaveInstanceState() will be called and because when it is called, it is called before onPause().

3关于 assets: Files in assets/ are not given a resource ID, so you can read them only using AssetManager

 

4

android.app.NotificationManager

 

这个类用一下几种方式来告诉user 在device的 background发生了些什么事情

 

        A persistent icon that goes in the status bar and is accessible through the launcher, (when the user selects it, a designated Intent can be launched),

  • Turning on or flashing LEDs on the device, or
  • Alerting the user by flashing the backlight, playing a sound, or vibrating.

每一个notify methods会以一个int 型的id 作为参数。这个id是作为notification唯一的标识(因为系统中会有很多的notification),当你用同样的id调用一个notify methods
的时候,相当于更新这个notification。你可以通过getSystemService(String)获得一个android.app.NotificationManager的实例对象

 

5在android中context可以作很多操作,但是最主要的功能加载 和访问资源 。在android中有两种context,一种是 application context,一种是activity context,

通常我们在各种类和方法间传递的是activity context。
比如一个activity的onCreate:
protected void onCreate(Bundle state) {
        super.onCreate(state);

        TextView label = new TextView(this); //传递context给view control
        label.setText("Leaks are bad");

        setContentView(label);
}
把activity context传递给view,意味着view拥有一个指向activity的引用,进而引用activity占有的资源:view hierachy, resource等。

 

 

5 Intent 与 pendingIntent的区别

intent英文意思是意图,pending表示即将发生或来临的事情。 
PendingIntent这个类用于处理即将发生的事情。比如在通知Notification中用于跳转页面,但不是马上跳转。 


Intent 是及时启动,intent 随所在的activity 消失而消失。 
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。 
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。

 

6 RemoteViews 就是一个可以显示在其它进程中对象

 

7 (1)Service生命周期只有onCreate, onStart和onDestroy,没有onResume, onPause和onStop 。如果你在onCreate或onStart做一些很耗时间的事情,最好启动一个线程来完成,因为如果Service是跑在主线程中的,会影响到你的UI操作或者阻塞主线程中的其他事情。BroadcastReceiver只能通过startService启动Service ,因为广播本身生命周期很短,bind的话没有意义\

8 HandlerThread

    Handy class for starting a new thread that has a looper.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics