ProgressDialog中的Context注意事项

第一个参数必须是目前运行的Activity的Context。

ProgressDialog类继承自AlertDialog类,同样存放在android.app包中。ProgressDialog有两种形式,一种是圆圈旋转形式,一种是水平进度条形式,选择哪种形式可以通过以下两个属性值来设定:

 

static intSTYLE_HORIZONTAL
          Creates a ProgressDialog with a horizontal progress bar.
static intSTYLE_SPINNER
          Creates a ProgressDialog with a ciruclar, spinning progress bar.

注意,当设置为水平进度条形式时,进度的取值范围为0—10000。

 

ProgressDialog的构造方法有以下两种:

 

ProgressDialog(Context context)
           
ProgressDialog(Context context, int theme)
           

 

 除了构造方法外,ProgressDialog还提供的如下的静态方法返回ProgressDialog对象:

 

static ProgressDialogshow(Context context,CharSequence title, CharSequence message)
           
static ProgressDialogshow(Context context,CharSequence title, CharSequence message, boolean indeterminate)
           
static ProgressDialogshow(Context context,CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable)
           
static ProgressDialogshow(Context context,CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable, DialogInterface.OnCancelListener cancelListener)

 

需要留意的是第一个参数必须是目前运行的Activity的Context。

android的ProgressDialog必须要在后台程序运行完毕前,以dismiss()方法来关闭取得焦点的对话框,否则程序就会陷入无法终止的无穷循环中。在线程中,不得有任何更改Context或parent View的任何状态,文字输出等时间,因为线程里的Context与View并不属于parent,两者之间也没有关联。

关于context的定义:

Putting it simply:

As the name suggests, its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application)

You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).

Typical uses of context:

  • Creating New objects: Creating new views, adapters, listeners:

    TextView tv = new TextView(getContext());
    ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
  • Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:

    context.getSystemService(LAYOUT_INFLATER_SERVICE)   
    getApplicationContext().getSharedPreferences(*name*, *mode*);
  • Accessing Components Implicitly:Regarding content providers, broadcasts, intent

    getApplicationContext().getContentResolver().query(uri, ...);

Definition of Context::

  • Context represents environment data
  • It provides access to things such as databases

Simpler terms ::

  • Consider Person-X is the CEO of a start-up software company

  • There is a lead architect present in the company, this lead architectdoes all the work in the company which involves such as database, UIetc

  • Now the CEO Hires a new Developer

  • It is the Architect who tells the responsibility of the new hiredperson based on the skills of the new person that whether he willwork on Database or UI etc

Simpler terms ::

  • It's like access of android-activity to app's resource

  • It's similar to a when you visit a hotel, you want breakfast, lunch & dinner in suitable timings right ?

  • There are many other things you like during the time of stay. How youget these things?

  • You ask the room-service person to bring the things for you

  • Here room-service person is the context considering you are thesingle activity and the hotel to be your app, finally the breakfast, lunch &dinner to be the resources


Things that involve context are:

  1. Loading a resource.
  2. Launching a new activity.
  3. Creating views.
  4. obtaining system service.

Context is the base class for Activity, Service, Application .... etc

Another way to describe is, consider context as remote of a TV & channel's in the television are resources, services, using intents etc - - - Here remote acts as an access to get access to all the different resources into foreground.

  • So, Remote has access to channels such as resources, services, using intents etc ....
  • Likewise ..... Whoever has access to remote naturally has access to all the things such as resources, services, using intents etc

Different invoking methods by which you can get context

  • getApplicationContext()
  • getContext()
  • getBaseContext()
  • or this (when in the activity class)

Example:

TextView TV=new TextView(this);

this -> refers to the context of the current activity.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值