我正在开发一个应用程序,我需要以下运行时权限才能启动服务:
@TargetApi(Build.VERSION_CODES.M)
public void startFloatyForAboveAndroidL() {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, PERMISSION_REQUEST_CODE);
} else {
floaty.startService();
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PERMISSION_REQUEST_CODE) {
if (Settings.canDrawOverlays(this)) {
floaty.startService();
} else {
Spanned message = Html.fromHtml("Please allow this permission, so <b>Floaties</b> could be drawn.");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
但是当我从bindView()
适配器启动服务时,我不知道在哪里编写这段代码。我在这里使用FastAdapter。
这是HRequest.java
文件的代码:
public class HRequest extends AbstractItem<HRequest, HRequest.ViewHolder> {
public String imageURL;
public HRequest() {
}
public HRequest(String imageURL) {
this.imageURL = imageURL;
}
// Fast Adapter methods
@Override
public int getType() {
return R.id.recycler_view;
}
@Override
public int getLayoutRes() {
return R.layout.h_request_list_row;
}
@Override
public void bindView(ViewHolder holder) {
super.bindView(holder);
holder.imageURL.setText(imageURL);
}
// Manually create the ViewHolder class
protected static class ViewHolder extends RecyclerView.ViewHolder {
TextView imageURL;
public ViewHolder(View itemView) {
super(itemView);
imageURL = (TextView)itemView.findViewById(R.id.imageURL);
if (!imageURL.getText().toString().isEmpty()) {
if (imageURL.getText().toString().startsWith("https://firebasestorage.googleapis.com/") || imageURL.getText().toString().startsWith("content://")) {
Picasso.with(itemView.getContext())
.load(imageURL.getText().toString())
.into(homelessImage);
} else {
Toast.makeText(itemView.getContext(), "some problem", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(itemView.getContext(), "no imageUID found", Toast.LENGTH_SHORT).show();
}
}
}
}
请让我知道如何以及如何编写此代码以请求许可并防止应用程序在 Android M 上崩溃。