1

好的,所以我最近完成了一个 android 应用程序 - 我的第一个应用程序:D!- 因为它是一个付费应用程序,谷歌告诉我我必须添加许可的东西。这很好,花花公子,除了在过去的五个小时里我一直被它吓坏了。最后认为我对它有点了解并开始使用它,但是在我的模拟器中对其进行测试时,我提出了两个问题:

我正在使用 google API 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);
}

}

我的第一个问题:为什么它没有按照应有的方式连接到服务器?我按照他们的教程构建了我的测试设备。我已经翻了三遍了!

第二个问题:当我在模拟器上启动应用程序时,连接错误对话框显示正常。我点击重试,它的工作原理。我点击取消,它杀死了应用程序。但是,如果我只是在对话框之外单击窗口中的任何其他位置,它会关闭对话框并且应用程序可以正常工作。那有点违背了整个目的,是吗?我该怎么做……不是这样?

以防万一我是个白痴并且错误出在其他地方,这是整个班级:

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,  android.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);
}

}


}
4

1 回答 1

2

对于您的第二个问题,我会将 setCanceledOnTouchOutside 设置为 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();
于 2013-08-02T20:52:10.333 回答