gpt4 book ai didi

java - Recyclerview 投入使用

转载 作者:太空宇宙 更新时间:2023-11-04 10:06:57 25 4
gpt4 key购买 nike

模态

public class Helper {
private String a;
private String b;
private String c;
private long d;
private long e;

public Helper() {
}

public Helper(String a, String b, String c, long d, long e) {
this.a= a;
this.b= b;
this.c= c;
this.d = d;
this.e= e;
}

public String geta() {
return a;
}

public void seta(String a) {
this.a= a;
}

public String getb() {
return b;
}

public void setb(String b) {
this.b= b;
}

public String getc() {
return c;
}

public void setce(String c) {
this.c = c;
}

public long isd() {
return d;
}

public void setd(long d) {
this.d= d;
}

public long gete() {
return e;
}

public void sete(long e) {
this.e= e;
}

}

服务

public class PieOptions  extends Service {
WindowManager wm;
RelativeLayout ll;
LayoutInflater li;

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
final View myview;
li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
wm = (WindowManager) getSystemService(WINDOW_SERVICE);
myview = li.inflate(R.layout.service_pie, null);

ImageButton mClose = (ImageButton) myview.findViewById(R.id.close);


WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_INPUT_METHOD |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,// | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);

/////////////////////////Another params


params = new WindowManager.LayoutParams(
750,1250,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);

params.gravity = Gravity.CENTER | Gravity.CENTER;

wm.addView(myview, params);
params.x = 0;
params.y = 0;

final WindowManager.LayoutParams finalParameters = params;

mClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
wm.removeView(myview);
stopSelf();
}
});
}

@Override
public void onDestroy() {
super.onDestroy();
stopSelf();
}

我已经进步这么多了。有人可以帮我继续吗?我想从 firebase 获取数据并将其显示在服务内的 recyclerview 中。谷歌上关于服务和膨胀布局的内容很少,所以我不太知道如何继续,所以有人可以帮助我编写代码吗?提前致谢

最佳答案

首先,如果你运行在API>=26中,你需要使用前台服务,使用下面的代码来启动你的服务

Intent intent = new Intent(getApplicationContext(), PieOptions.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
}else {
startService(intent);
}

现在在 PieOptions 类中,首先扩展 JobIntentService 而不是 Service,在 onCreate() 方法中添加以下代码。前台服务应在 API >= 26 上向用户显示通知,否则服务将在 5 秒内终止。

   @Override
public void onCreate() {
super.onCreate();
String CHANNEL_ID = "my_service";
String CHANNEL_NAME = "My Background Service";
if (Build.VERSION.SDK_INT >= 26) {
// in APIs 26+ we should show a notifications
NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setCategory(Notification.CATEGORY_SERVICE).setPriority(PRIORITY_MIN).build();

startForeground(NOTIFICATION_ID, notification);
}

}

您需要做的另一件事是修改初始化布局参数时使用的类型,以下代码将根据您的 API 版本为您提供正确的类型

int type = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}

现在最后一件事,要绘制叠加层,您需要确保您有权绘制叠加层,

private boolean canDrawOnScreen() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return canDrawOnScreenM();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return canDrawOverlaysUsingReflection(getApplicationContext());
} else
return true;
}

@RequiresApi(api = Build.VERSION_CODES.M)
private boolean canDrawOnScreenM() {
return Settings.canDrawOverlays(getApplicationContext());
}


@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean canDrawOverlaysUsingReflection(Context context) {
try {
AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
Class clazz = AppOpsManager.class;
Method dispatchMethod = clazz.getMethod("checkOp", new Class[]{int.class, int.class, String.class});
//AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
int mode = (Integer) dispatchMethod.invoke(manager, new Object[]{24, Binder.getCallingUid(), context.getApplicationContext().getPackageName()});
return AppOpsManager.MODE_ALLOWED == mode;
} catch (Exception e) {
return false;
}

}

现在对于膨胀部分和处理回收器 View ,首先您需要对回收器 View 的引用,您可以执行以下操作,

                RecyclerView myRecyclerView = mView.findViewById(R.id.your_recycler_id);

然后你可以像往常一样处理你的RecyclerView,设置布局管理器和适配器。

关于java - Recyclerview 投入使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52800735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com