在最新的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) {
…..
}
RSS