Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> Android m5到0.9版之間API改變分析

Android m5到0.9版之間API改變分析

編輯:初級開發

在最新的android SDK 0.9版中很多地方都有了改變,涉及改變的API我們已經在android移植欄目中介紹到,下面以更詳細的代碼實例形式介紹,不斷更新增加: (比如IntentReceiver改名為BroadcastReceiver這種更合適的名稱),我們看到android 0.9 SDK中API的變化在改變上主要是命名方式更准確,實現過程改變不是很多。

IntentReceiver renamed to BroadcastReceiver


Detailed Problem Description:

For example, if you have a IntentReceiver class name MyReceiver..

In androidManifest.XML

Error: MyReceiver does not extend android.context.BroadcastReceiver

In MyReceiver class defination

Error: Cannot resolve type IntentReceiver


Solution:

replace

import android.content.IntentReceiver;

public class MyReceiver extends IntentReceiver
{
    @Override
    public void onReceiveIntent(Context context, Intent intent)
    {
    }

}

with
import android.content.BroadcastReceiver;

 

public class MyReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
    }

}


Notes:



onFreeze() renamed to onSaveInstanceState()

Detailed Problem Description:

replace
@Override
protected void onFreeze(Bundle outState) {
super.onFreeze(outState);
}

Solution:
with
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
Notes:

 



startSubActivity() renamed to startActivityForResult()

Detailed Problem Description:

replace

Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
startSubActivity(i, ACTIVITY_EDIT);

Solution:

with

Intent i = new Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID, id);
startActivityForResult(i, ACTIVITY__EDIT);
Notes:


Limits on resources available to application

Detailed Problem Description:

Error: Resource is not public..
Some resources have been made private in the latest release..
Only resources needed for application development are left public.

Solution:

Check the public resources @ docs/reference/android/package-summary.Html

Notes:

 


Layout attributes renamed

Detailed Problem Description:

Some XML attributes are renamed, removed..and new attributes are added.

Solution:

Use the new auto-complete feature in Eclipse for yourlayout.XML files
and choose from the available attributes. Use the new layout editor
(the first tab when you click on your layout XML files )
to debug and check your vIEws/layouts.

Notes:


Integer types not allowed at layout_xxx,spacing,padding,etc

Detailed Problem Description:

It is required to specify the unit of measurement for layout attributes.
Only numeric values are no longer enough, you will receive an error
indicating "Integer types not allowed"

Solution:

Specify unit..
For example:
replace
<VIEw layout_width="10" />
with
<VIEw layout_width="10dp" />

Notes:


MapVIEw crashes

Detailed Problem Description:

You will notice the following errors while using MapVIEw:

1) ClassNotFound exceptions while using MapVIEw.

2) Java.lang.IllegalArgumentException: You need to specify an API Key for each MapVIEw


Solution:

For one, Maps API have now been moved into their own separate shared library.

Add the following tag to your androidManifest.XML to fix this issue:

<uses-library android:name="com.google.android.maps" />        

For the second issue, you will now need a Api key to use MapVIEw, for now
it can be any random string. Add android:apiKey="apisamples"
attribute to you MapVIEw tag in layout XML file.

Notes:

See ApiDemos -> view/MapVIEwDemo sample code.



Cannot re-install ApiDemos

Detailed Problem Description:

You will notice a signing error when you try to re-install ApiDemos for the first time.


Solution:

Refer to:
http://code.google.com/android/kb/troubleshooting.Html#apidemosreinstall


Notes:


requestUpdates() is undefined for LocationManager

Detailed Problem Description:

The LocationManager class does not fire Location update Intents. The
requestUpdates method has been removed. For using mock LocationProviders ,
you can no longer provide canned LocationProviders
in the /system/etc/location directory


Solution:

The LocationManager class now notifIEs LocationListener objects of
location and status changes, rather than firing Intents. The
requestUpdates method has been renamed to requestLocationUpdates and
now takes a LocationListener object rather than an Intent. A new
requestStatusUpdates method has been added, also taking a
LocationListener object. The removeUpdates method now takes a
LocationListener object.

Notes:

For more information refer to:
http://code.google.com/android/toolbox/apis/lbs.Html

A sample app for using Location Apis can be found in the files section in
android-developer forums.


 



Cursor.putXxx() and Activity.managedCommitUpdates() are deprecated

Detailed Problem Description:

You will notice that the Cursor.putXxx() methods and the Activity.managedCommitUpdates() are deprecated.

Solution:

replace with calls to ContentResolver:


ContentValues values = new ContentValues();
values.put(Notes.MODIFIED_DATE, System.currentTimeMillis());
values.put(Notes.TITLE, title);
values.put(Notes.NOTE, text);
// Commit all of our changes to persistent storage. When the update completes
// the content provider will notify the cursor of the change, which will
// cause the UI to be updated.
getContentResolver().update(mUri, values, null, null);


Notes:

See NoteEditor.Java in the NotePad sample for an example of usage.



 

Menu.Item renamed to Menu.MenuItem

Detailed Problem Description:

replace:
public boolean onOptionsItemSelected(Menu.Item item) {
        switch (item.getId()) {
        .....

Solution:

with:

public boolean onOptionsItemSelected(Menu.MenuItem item) {
        switch (item.getItemId()) {
        .....


Notes:

Also, menu.add() methods now take new parameter "order"



setResult() now takes Intent instead of string

Detailed Problem Description:

replace:
1) setResult(RESULT_OK, "Corky!");

2) protected void onActivityResult(int requestCode, int resultCode,
        String data, Bundle extras) {
         .....
    }

Solution:

with:

1) Bundle bundle = new Bundle();
    bundle.putString(TEST_STRING, "Corky!");
    Intent mIntent = new Intent();
    mIntent.putExtras(bundle);
    setResult(RESULT_OK, mIntent);


2) protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
        .....
    }
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved