我必须使用一个OnGlobalLayoutListener对象,然后删除监听器,我有一个问题与我用以下代码解决的弃用方法.
protected void onCreate(Bundle savedInstanceState) {
final LinearLayout llTotal = (LinearLayout) findViewById(R.id.mmc_ll);
ViewTreeObserver vto = llTotal.getViewTreeObserver();
if(vto.isAlive()){
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//
// mycode
//
if (Build.VERSION.SDK_INT<16) {
removeLayoutListenerPre16(llTotal.getViewTreeObserver(),this);
} else {
removeLayoutListenerPost16(llTotal.getViewTreeObserver(), this);
}
}
});
}
super.onCreate(savedInstanceState);
}
@SuppressWarnings("deprecation")
private void removeLayoutListenerPre16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
observer.removeGlobalOnLayoutListener(listener);
}
@TargetApi(16)
private void removeLayoutListenerPost16(ViewTreeObserver observer, OnGlobalLayoutListener listener){
observer.removeOnGlobalLayoutListener(listener);
}
Run Code Online (Sandbox Code Playgroud)
这是对的吗?有更好的方法来处理兼容性?
使用API 10在模拟器中运行代码我在LogCat中有以下警告
04-24 09:30:12.565: I/dalvikvm(471): Could not find method android.view.ViewTreeObserver.removeOnGlobalLayoutListener, referenced from method com.my.project.ActivityHome.removeLayoutListenerPost16
04-24 …Run Code Online (Sandbox Code Playgroud) 当我使用Android KitKat尝试我的应用程序时,我在PreferenceActivity中出错.
PreferenceActivity的子类必须覆盖isValidFragment(String)以验证Fragment类是否有效!com.crbin1.labeltodo.ActivityPreference尚未检查片段com.crbin1.labeltodo.StockPreferenceFragment是否有效
在文档中,我找到以下解释
protected boolean isValidFragment(String fragmentName)
在API级别19中添加
子类应覆盖此方法并验证给定片段是否要附加到此活动的有效类型.对于为Android构建的应用程序,默认实现返回true:比KITKAT更早的targetSdkVersion.对于更高版本,它将抛出异常.
我找不到任何解决问题的例子.
在Android 4.4 API页面中,我读到:
将应用程序的targetSdkVersion设置为"19"或更高时,使用set()或setRepeating()创建的警报将不准确.
[切]
这种不精确的批处理行为仅适用于更新的应用程序.如果您将targetSdkVersion设置为"18"或更低,则在Android 4.4上运行时,您的警报将继续像以前的版本一样运行.
在我的应用程序中,我需要准确的时间警报,我将targetSdkVersion更新为"19".以下代码是否正确?另外,如果我在手机中将targetSdkVersion设置为"19",那么"旧"方法AlarmManager.set会以同样的方式继续工作吗?
private void setAlarm (long ms){
final AlarmManager am = (AlarmManager) mCtx.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, BroadcastReceiverAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(this, ALARM_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT<Build.VERSION_CODES.KITKAT) {
am.set(AlarmManager.RTC, ms, pi);
} else {
setAlarmFromKitkat(am, ms, pi);
}
}
@TargetApi(19)
private void setAlarmFromKitkat(AlarmManager am, long ms, PendingIntent pi){
am.setExact(AlarmManager.RTC, ms, pi);
}
Run Code Online (Sandbox Code Playgroud) 今天我有一个奇怪的错误,很奇怪,因为应用程序前一天工作,我没有改变任何东西,我也更新了Android SDK和eclipse.
05-20 08:03:14.217:E/AndroidRuntime(1925):java.lang.NoClassDefFoundError:android.support.v4.app.NotificationCompat $ Builder
05-20 08:03:15.918:E/dalvikvm(1716):找不到类'android.support.v4.app.NotificationCompat $ Builder',从方法com.me.app.ObjReminder.setNotification中引用
为什么我这个错误?我该如何解决?
编辑:谢谢大家,我解决了'订单和导出'选项卡中的'Android私有库'的问题.但我不明白为什么检查消失了
CursorAdapter有3个构造函数.让我们看一下指南和参考.
1)CursorAdapter(上下文上下文,光标c)
不推荐使用此构造函数.不鼓励使用此选项,因为它会导致在应用程序的UI线程上执行Cursor查询,从而导致响应能力较差甚至应用程序无响应错误.或者,使用带有CursorLoader的LoaderManager.
2)CursorAdapter(Context context,Cursor c,boolean autoRequery)
允许控制自动重新查询的构造函数.建议您不要使用它,而是使用 CursorAdapter(Context,Cursor,int).
3)CursorAdapter(上下文上下文,Cursor c,int标志)
推荐的构造函数.
flags 用于确定适配器行为的标志; 可以是FLAG_AUTO_REQUERY和FLAG_REGISTER_CONTENT_OBSERVER的任意组合.
FLAG_AUTO_REQUERY 不推荐使用此常量.不鼓励使用此选项,因为它会导致在应用程序的UI线程上执行Cursor查询,从而导致响应能力较差甚至应用程序无响应错误.或者,使用带有CursorLoader的LoaderManager.
FLAG_REGISTER_CONTENT_OBSERVER.将CursorAdapter与CursorLoader一起使用时,不需要此标志.
CursorAdapter(Context context,Cursor c,int flags)是推荐的构造函数,但是可能的标志是2,当使用带有CursorLoader的CursorAdapter时,不推荐使用其中一个,而不需要另一个.如果我使用带有CursorLoader的CursorAdapter,我必须使用此构造函数并将零作为标志传递?在这种情况下,构造函数是否与#1相同?
android constructor android-loadermanager android-cursoradapter
有一个带有Listview的AppWidget我想用RemoteViewsService.RemoteViewsFactory填充.
我有以下错误
E/AndroidRuntime(1203):java.lang.SecurityException:Permission Denial:读取com.crbin1.myapp.data.ProviderLTD uri内容://com.crbin1.myapp.data.ProviderLTD/TB_WIDGET3P来自pid = 413,uid = 10013要求提供者被导出,或者grantUriPermission()
如果我在清单中导出ProviderLTD(我的内容提供商),它可以工作,但我想避免导出它.
另一个解决方案是grantUriPermission().我在清单中将grantUriPermissions设置为true,但现在我不明白我的代码中我必须授予权限.
public class Widget3pService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new StackRemoteViewsFactory(this.getApplicationContext(), intent);
}
}
class StackRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {
private List<ObjW3Task> mWidgetItems = new ArrayList<ObjW3Task>();
private Context mContext;
private int mAppWidgetId;
public StackRemoteViewsFactory(Context context, Intent intent) {
mContext = context;
mAppWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}
public RemoteViews getViewAt(int position) {
// use mWidgetItems to populate ListView
// fillInIntent for listeners
return myRemoteViews;
}
public void …Run Code Online (Sandbox Code Playgroud) 我有一个3行_id,名称和数字的表.我必须更新具有不同值的每一行的列号.Actualy我的代码是:
ContentValues values;
ContentResolver cr = getContentResolver();
int n[] = new int[10];
// functions to assign the values to n[]
for(int i=1; i<31;i++) {
values = new ContentValues();
values.put("number", n[i]);
cr.update(MyProvider.CONTENT_URI_TABLE, values, "_id="+i, null);
}
Run Code Online (Sandbox Code Playgroud)
通过这种方式,我必须使用大量的UPDATE和多次访问数据库.有一种方法可以简化程序吗?
我需要创建2个通知,但如果我使用FLAG_UPDATE_CURRENT或FLAG_CANCEL_CURRENT,那么它们都会获得相同的额外通知,否则为第一个.
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
for(int num=1;num<3;num++){
Intent intent = new Intent(this, ActivityNotification.class);
intent .putExtra("reminder_id", num);
PendingIntent pi = PendingIntent.getActivity(this, num, intent , 0);
// PendingIntent pi = PendingIntent.getActivity(this, num, intent , PendingIntent.FLAG_UPDATE_CURRENT);
Notification noti = new Notification.Builder(this)
.setContentIntent(pi)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.noti)
.setWhen(when)
.setAutoCancel(true)
.build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(num, noti);
}
Run Code Online (Sandbox Code Playgroud)
我怎么能有不同的额外内容?或者一般来说,我的ActivityNotification如何知道哪个通知称为它?
在我的应用程序中,我通过SQLiteOpenHelper对象直接访问数据库.
现在我想使用ContentProviders.在数据库中,我有14个表和6个视图.以下之间的最佳做法是什么?
一个内容提供程序,用于访问所有表和视图
每个表和视图的一个内容提供者(共20个内容提供者)
每组表和视图的一个内容提供程序(总计5/6内容提供程序)
android ×9
alarmmanager ×1
constructor ×1
database ×1
deprecated ×1
sdk ×1
sql-update ×1
sqlite ×1