有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何强制用户在允许访问活动之前处理对话框?我的许可证代码怎么了?

好吧,我最近完成了一个安卓应用程序——我的第一个:D!-因为这是一款付费应用,谷歌告诉我必须添加授权内容。那很好,很漂亮,只是过去五个小时我一直被它弄得心烦意乱。最后,我想我理解了一点,并开始使用它,但在我的模拟器中进行测试时,我发现了两个问题:

我正在使用GoogleAPI for 4.1,正如他们在开发者控制台上便捷的操作指南所指示的那样,但不管怎样,我总是会出现连接错误对话框。代码如下:

public void dontAllow(int reason) {
    if(isFinishing()){
        return;
    }
    displayResults("Access Denied");

    if(reason==Policy.RETRY){
        showDialog(DIALOG_RETRY);
    }else{
        showDialog(DIALOG_ERROR);
    }
}

public void applicationError(int errorCode) {
dontAllow(0);
}

public void displayResults(String result){

}

和正在调用的对话框:

protected Dialog onCreateDialog(int id){
switch(id){
case DIALOG_RETRY:
    Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Connection Error:  Retry?");
    builder.setCancelable(true);
    builder.setPositiveButton("Retry", new RetryOnClickListener());
    builder.setNegativeButton("Cancel", new CancelOnClickListener());
    AlertDialog dialog = builder.create();
    dialog.show();
break;
case DIALOG_ERROR:
    Builder builder1 = new AlertDialog.Builder(this);
    builder1.setMessage("Would you like to purchase Landscape ID! ?");
    builder1.setCancelable(true);
    builder1.setPositiveButton("Yes!", new BuyOnClickListener());
    builder1.setNegativeButton("No.", new CancelOnClickListener());
    AlertDialog dialog1 = builder1.create();
    dialog1.show();
    break;
}
return super.onCreateDialog(id);

}

private final class RetryOnClickListener implements 
DialogInterface.OnClickListener{

public void onClick(DialogInterface dialog, int which) {
    checker.checkAccess(checkerCB);
}

}

private final class CancelOnClickListener implements
DialogInterface.OnClickListener{

public void onClick(DialogInterface dialog, int which) {
    onDestroy();
    finish();
}

}
private final class BuyOnClickListener implements
DialogInterface.OnClickListener{

public void onClick(DialogInterface dialog, int which) {
    Intent open = new Intent(Intent.ACTION_VIEW);
    open.setData(Uri.parse("market://details?       id=com.mustaroenterprise.landscapeid"));
    startActivity(open);
}

}

我的第一个问题:为什么它没有像应该的那样连接到服务器?我已经按照他们的指导构建了我的测试平台。我已经看了三遍了

第二个问题:当我在模拟器上启动应用程序时,“连接错误”对话框显示良好。我点击“重试”,它就工作了。我点击Cancel,它会关闭应用程序。但是,如果我只需单击窗口中对话框外的任何其他位置,它就会关闭对话框,应用程序就会正常工作。那样会破坏整个目的,是吗?我该怎么做。。。不是吗

以防万一我是个白痴,错误就在别处,下面是全班同学:

private static final int DIALOG_RETRY = 10;
private static final int DIALOG_ERROR=20;
private static final byte[] SALT =      {1,2,3,4,5,6,72,88,-37,-55,-23,34,22,14,15,16,17,18,19,-20};
private static final String BASE64_PUBLIC_KEY =    "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqvh1xrNmvio909T06vAUxW3rtc98E3xNLA6qR/zdq2zHNW tQUJkDmJGukrkWj4Vd38NiD+nW92MX3HY2/dfw4AIcwS2oyeYceYc3hi4y2KeVL84y3DrOO0fCKNqBr6/Ve0cefN9HVyy57Psl4B0y8OaG9500xuEUeguO+PyIAMqFrtHVyi/seimnrcYLTYJo9IfGTRhcwi6QqQE8OlplidaT+uYwR4hNfcNLbnWnr7xDeG5gL2usibFPg+cvhFVhIGKO/aFuAVUIH2Yoarudc888X3/ZjTbmYAGuGhS8GRxiHhTVknCznX3BcxBJNeMA+xPTZ4OnaryRkHVvoJx5WQIDAQAB";
private LicenseCheckerCallback checkerCB;
private LicenseChecker checker;
TextView title, description, cure;
ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //LICENSING
    checkerCB = new CallBack();
    final TelephonyManager tm = (TelephonyManager)     getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
    checker = new LicenseChecker(
            this, new ServerManagedPolicy(this,
                    new AESObfuscator(SALT,     getPackageName(),tm.getDeviceId())),
                    BASE64_PUBLIC_KEY);

    checker.checkAccess(checkerCB);

    //END LICENSING
    setContentView(R.layout.activity_main);
    title = (TextView)findViewById(R.id.title);
    description = (TextView)findViewById(R.id.description);
    image = (ImageView)findViewById(R.id.image);
    cure =(TextView)findViewById(R.id.cure);
    Spinner dropdown = (Spinner)findViewById(R.id.mainMenu);

    //List Menu Items
  final String options[] = {
            //TURF DISEASES
            "-Turf Diseases-", "Dollar Spot","Red Thread","Pythium Blight",     "Necrotic Ring","Summer Patch","Brown Patch","Fairy Ring"
            ,"White Patch","Rust"

            //TURF INSECTS
            ,"-Turf Insects-","Chinch Bug","Army Worm","Hunting    Billbug","Aphid","Black Cutworm","Leaf Hopper","White Grub"

            //ORNAMENTAL DISEASES
            ,"-Ornamental Diseases-","Powdery Mildew","Leaf Spot"

            //ORNAMENTAL INSECTS
            ,"-Ornamental Insects-","Aphid","Leaf Miner","Japanese Beatle","Spider Mites","White Fly","Euonymus Scale","Web Worm"

            };

    //End List Menu Items

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  安卓.R.layout.simple_spinner_item,options);
    dropdown.setAdapter(adapter);

    dropdown.setOnItemSelectedListener(new OnItemSelectedListener(){

        public void onItemSelected(AdapterView<?> parent, View v,
                int position, long id) {
            newSelection(options[position]);

        }
        public void onNothingSelected(AdapterView<?> arg0) {
        } 
   });
   }

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
 public void newSelection(String selection){
        if(!selection.contains("-")){
            title.setText(selection);
            selection=selection.replace(" ", "_");
            selection=selection.toUpperCase();
            description.setText(getResourceID("DESC_"+selection,  R.string.class));
            image.setImageResource(getResourceID(selection.toLowerCase(), R.drawable.class));

     }else{
         title.setText("Select a disease or insect.");
         description.setText("");
         cure.setText("");
         image.setImageResource(getResourceID("logo", R.drawable.class));
     }
 }
 @SuppressWarnings("rawtypes")
 public int getResourceID(String name, Class resType){

     try{
         Class res = null;
         if(resType == R.drawable.class)
             res=R.drawable.class;
         if(resType==R.id.class)
             res=R.id.class;
         if(resType==R.string.class)
             res=R.string.class;
         java.lang.reflect.Field field = res.getField(name);
                 int retID = field.getInt(null);
         return retID;
     }catch(Exception e){
    }return 0;
}
 protected void onResume() {
    newSelection("-");
    super.onPause();
}
 protected void onDestroy(){
    super.onDestroy();
    checker.onDestroy();
}
//LICENSING CALLBACK CLASS
private class CallBack implements LicenseCheckerCallback{

public void allow(int reason) {
    if(isFinishing()){
        return;
    }
    displayResults("Access Granted");
}

public void dontAllow(int reason) {
    if(isFinishing()){
        return;
    }
    displayResults("Access Denied");

    if(reason==Policy.RETRY){
        showDialog(DIALOG_RETRY);
    }else{
        showDialog(DIALOG_ERROR);
    }
}

public void applicationError(int errorCode) {
dontAllow(0);
}

public void displayResults(String result){

}

}

//DIALOG CLASS AND ACTION LISTENERS
protected Dialog onCreateDialog(int id){
switch(id){
case DIALOG_RETRY:
    Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Connection Error:  Retry?");
    builder.setCancelable(true);
    builder.setPositiveButton("Retry", new RetryOnClickListener());
    builder.setNegativeButton("Cancel", new CancelOnClickListener());
    AlertDialog dialog = builder.create();
    dialog.show();
break;
case DIALOG_ERROR:
    Builder builder1 = new AlertDialog.Builder(this);
    builder1.setMessage("Would you like to purchase Landscape ID! ?");
    builder1.setCancelable(true);
    builder1.setPositiveButton("Yes!", new BuyOnClickListener());
    builder1.setNegativeButton("No.", new CancelOnClickListener());
    AlertDialog dialog1 = builder1.create();
    dialog1.show();
    break;
}
return super.onCreateDialog(id);

}

private final class RetryOnClickListener implements 
DialogInterface.OnClickListener{

public void onClick(DialogInterface dialog, int which) {
    checker.checkAccess(checkerCB);
}

}

private final class CancelOnClickListener implements
DialogInterface.OnClickListener{

public void onClick(DialogInterface dialog, int which) {
    onDestroy();
    finish();
}

}
private final class BuyOnClickListener implements
DialogInterface.OnClickListener{

public void onClick(DialogInterface dialog, int which) {
    Intent open = new Intent(Intent.ACTION_VIEW);
    open.setData(Uri.parse("market://details?  id=com.mustaroenterprise.landscapeid"));
    startActivity(open);
}

}


}

共 (1) 个答案

  1. # 1 楼答案

    对于第二个问题,我会将setCanceledOnTouchOut设置为false,如下所示:

    Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Connection Error:  Retry?");
    builder.setCancelable(true);
    builder.setPositiveButton("Retry", new RetryOnClickListener());
    builder.setNegativeButton("Cancel", new CancelOnClickListener());
    AlertDialog dialog = builder.create();
    
    //Add this
    dialog.setCanceledOnTouchOutside(false);
    
    dialog.show();