在groovy文件中,我导入了jars private void getContact(){
// Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
// startActivityForResult(intent, PICK_CONTACT);
}
public void askForContactPermission(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_CONTACTS)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Contacts access needed");
builder.setPositiveButton(android.R.string.ok, null);
builder.setMessage("please confirm Contacts access");//TODO put real question
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onDismiss(DialogInterface dialog) {
requestPermissions(
new String[]
{Manifest.permission.READ_CONTACTS}
, PERMISSION_REQUEST_CONTACT);
}
});
builder.show();
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_CONTACTS},
PERMISSION_REQUEST_CONTACT);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}else{
getContact();
}
}
else{
getContact();
}
}
int PERMISSION_REQUEST_CONTACT = 1;
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// getContact();
Toast.makeText(getApplicationContext(),"Permission is Done",Toast.LENGTH_LONG).show();
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
Toast.makeText(getApplicationContext(),"No permission for contacts",Toast.LENGTH_LONG).show();
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
和groovy.util.logging.Log4j
以使用
org.apache.log4j.Level
,我将日志级别设置为: @Log4j
在构造函数中。
现在执行脚本时我得到log.setLevel(Level.TRACE)
但是当我用 INFO 替换 TRACE 时,它会成功执行。
那么这是什么原因?
答案 0 :(得分:1)
您是否在应用程序中使用了多个版本的log4j? [log4j-1.2.16.jar&降低到不支持TRACE的log4j-1.2.12.jar] 在这种情况下,你的groovy脚本可能已经考虑了较低版本。如果你在LINUX中执行脚本,最后将$ GROOVY_HOME / lib / log4j-1.2.16.jar附加到$ CLASSPATH。这有效!!!