Android SDK
在版本升级的过程中做了一些API的修改,如果我们不清楚这些改变就不知道新版本API如何调用了,我今天就遇到一个问题,找各种资料看,发现很多人收藏的一些资料貌似很新,实际上都是很老的资料,我都怀疑他们是不是试过就收藏了,几面仍然用的是很久的API调用方法。下面是我收藏的一点点总结:
1).IntentReceiver 到
BroadcastReceiver的改变
假设你有一个 IntentReceiver 叫做
MyReceiver,在 AndroidManifest.xml文件中出现:
Error: MyReceiver does not
extend android.context.BroadcastReceiver,
并且在
MyReceiver类定义中出现:
Error: Cannot resolve type
IntentReceiver,
解决办法是:
替换 import
android.content.IntentReceiver:
public class MyReceiver
extends IntentReceiver
{
@Override
public void
onReceiveIntent(Context context, Intent intent)
{
}
}
为:
import
android.content.BroadcastReceiver;
public class MyReceiver
extends BroadcastReceiver
{
@Override
public void
onReceive(Context context, Intent intent)
{
}
}
2). onFreeze() 到
onSaveInstanceState()的改变
替换 @Override
protected void
onFreeze(Bundle outState) {
super.onFreeze(outState);
}
为:
@Override
protected void
onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
3). startSubActivity() 到
startActivityForResult()的改变
替换 Intent i = new
Intent(this, NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID,
id);
startSubActivity(i,
ACTIVITY_EDIT);
为:
Intent i = new Intent(this,
NoteEdit.class);
i.putExtra(NotesDbAdapter.KEY_ROWID,
id);
startActivityForResult(i,
ACTIVITY__EDIT);
4). Limits on resources
available to application
出现如下错误:
Error: Resource is not
public..
Some resources have been made
private in the latest release..
Only resources needed for
application development are left public.
解决办法:
检查 public resources @
docs/reference/android/package-summary.html
5). Layout attributes
renamed
问题描述:Some xml attributes are
renamed, removed..and new attributes are added.
解决办法: 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.
6). Integer types not allowed
at layout_xxx,spacing,padding,etc
问题描述: 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"。
解决办法: Specify
unit..
替换 <View
layout_width="10" />为:<View
layout_width="10dp" />
7). MapView crashesDetailed
Problem Description:
当你在使用MapView时看到:
1> ClassNotFound exceptions while using
MapView.
2> java.lang.IllegalArgumentException: You need to
specify an API Key for each MapView
解决办法:
对于第一个问题, Maps API 已经被移动到自有的
separate shared library,只需要把<uses-library
android:name="com.google.android.maps"
/>添加到你的
AndroidManifest.xml
文件就可以了;对于第二个问题,你需要一个 API key 来使用 MapView, key有可以是随机字符串,把属性
android:apiKey="apisamples"绑定到你的
layout xml 文件的
MapView.
8). See ApiDemos
-> view/MapViewDemo sample code. 当你第一次尝试 re-install
ApiDemos 的时候出现signing error信息,解决办法详见:
http://code.google.com/android/kb/troubleshooting.html#apidemosreinstall
9). requestUpdates() is
undefined for LocationManager
如下问题:
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。
解决办法: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
dded,
also taking a
LocationListener object. The removeUpdates method now takes a
LocationListener object.
10).
更多信息参见: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
问题:You will notice that the
Cursor.putXxx() methods and the Activity.managedCommitUpdates() are
deprecated.
解决办法: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);
11). See NoteEditor.java in
the NotePad sample for an example of usage.
Menu.Item renamed to
Menu.MenuItem
替换 public boolean
onOptionsItemSelected(Menu.Item item) {
switch (item.getId())
{
.....
为: public boolean
onOptionsItemSelected(Menu.MenuItem item) {
switch (item.getItemId())
{
.....
12). menu.add() methods now
take new parameter "order"
setResult() now takes Intent
instead of string
替换 1) setResult(RESULT_OK,
"Corky!");
2) protected void
onActivityResult(int requestCode, int resultCode,
String data, Bundle
extras) {
.....
}
为: 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)
{
.....
}
关于Android SDKI新版本中API修改的总结
最新推荐文章于 2023-05-19 19:07:27 发布
