Java 在允许访问活动之前,如何强制用户处理对话框?还有什么';我的许可证代码有什么问题?
好吧,我最近完成了一个android应用程序——我的第一个:D!-因为这是一款付费应用,谷歌告诉我必须添加授权内容。那很好,很漂亮,只是过去五个小时我一直被它弄得心烦意乱。最后,我想我理解了一点,并开始使用它,但在我的模拟器中进行测试时,我发现了两个问题: 我正在使用GoogleAPI for 4.1,正如他们在开发者控制台上便捷的操作指南所指示的那样,但不管怎样,我总是会出现连接错误对话框。代码如下:Java 在允许访问活动之前,如何强制用户处理对话框?还有什么';我的许可证代码有什么问题?,java,android,Java,Android,好吧,我最近完成了一个android应用程序——我的第一个:D!-因为这是一款付费应用,谷歌告诉我必须添加授权内容。那很好,很漂亮,只是过去五个小时我一直被它弄得心烦意乱。最后,我想我理解了一点,并开始使用它,但在我的模拟器中进行测试时,我发现了两个问题: 我正在使用GoogleAPI for 4.1,正如他们在开发者控制台上便捷的操作指南所指示的那样,但不管怎样,我总是会出现连接错误对话框。代码如下: public void dontAllow(int reason) { if(isF
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, 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);
}
}
}
private static final int DIALOG\u RETRY=10;
私有静态最终整数对话框_错误=20;
私有静态最后字节[]SALT={1,2,3,4,5,6,72,88,-37,-55,-23,34,22,14,15,16,17,18,19,-20};
私有静态最终字符串BASE64\u PUBLIC\u KEY="Miibijanbgkqhkig9w0baqefaaoq8amiibcgkcaqeaqvh1xrnmvio909t06vauxw3rtc98e3xnla6qr/zdq2zHNWTQUJKDMJGUKWJ4VD38NID+nW92MX3HY2/DFW4AICWS2OYYYYY3HI4KEVL84Y3DROO0FCKNQBR6/VE0CEFN9HVYY57PSL4B0Y8OAG9500Xueguo+PyamQFrthVyi/Seimnrcyltyjo9IFGTRHCWI6QQQQQE8OLPLIDAT+UYWR4CNFBN7XDEG5GL2USIBPG+cvhFVhIGKO/AFUAVU2YYOYOUARUDC883/ZJTBYGrX8ZKZKZN8ZKZKW8ZKZW8B4WWKZKZK8B8BQWW8B8B8B8B8B”;
私人持牌人支票回拨支票;
私人牌照审查员;
文本视图标题、描述、修复;
图像视图图像;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//许可证
checkerb=新回调();
final TelephonyManager tm=(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
checker=新许可证检查器(
这个,新的ServerManagedPolicy(这个,
新的AESObfuscator(SALT,getPackageName(),tm.getDeviceId()),
BASE64_公钥);
checker.checkercess(checkerCB);
//终端许可
setContentView(R.layout.activity_main);
title=(TextView)findViewById(R.id.title);
description=(TextView)findViewById(R.id.description);
image=(ImageView)findviewbyd(R.id.image);
cure=(TextView)findViewById(R.id.cure);
微调器下拉菜单=(微调器)findViewById(R.id.mainMenu);
//列出菜单项
最终字符串选项[]={
//草坪病害
-草坪病害-“,”美元斑“,”红线“,”腐霉枯萎“,”坏死环“,”夏季斑“,”棕色斑“,”仙女环“
“白斑”、“锈斑”
//草坪昆虫
,“草坪昆虫-”,“Chinch Bug”,“军虫”,“猎蝽”,“蚜虫”,“黑地老虎”,“叶蝉”,“白蛴螬”
//观赏病害
,“观赏性病害-,“白粉病”,“叶斑病”
//观赏昆虫
,“观赏昆虫-”,“蚜虫”,“潜叶虫”,“日本披头士”,“蜘蛛螨”,“白蝇”,“卫矛鳞片”,“网虫”
};
//结束列表菜单项
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器项目,选项);
下拉列表.setAdapter(适配器);
setOnItemSelectedListener(新的OnItemSelectedListener(){
已选择公共视图(AdapterView父视图、视图v、,
内部位置,长id){
新闻选举(选项[职位]);
}
未选择公共无效(AdapterView arg0){
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.activity\u主菜单);
返回true;
}
公共无效新闻选择(字符串选择){
如果(!selection.contains(“-”){
title.setText(选择);
选择=选择。替换(“,”);
selection=selection.toUpperCase();
description.setText(getResourceID(“DESC_”+selection,R.string.class));
setImageResource(getResourceID(selection.toLowerCase(),R.drawable.class));
}否则{
title.setText(“选择一种疾病或昆虫”);
description.setText(“”);
cure.setText(“”);
setImageResource(getResourceID(“logo”,R.drawable.class));
}
}
@抑制警告(“原始类型”)
public int getResourceID(字符串名称,类重新类型){
试一试{
类res=null;
if(resType==R.drawable.class)
res=R.可绘制类;
if(resType==R.id.class)
res=R.id.class;
if(resType==R.string.class)
res=R.string.class;
java.lang.reflect.Field=res.getField(名称);
int-retID=field.getInt(null);
返回retID;
}捕获(例外e){
}返回0;
}
受保护的void onResume(){
新闻选举(“—”);
super.onPause();
}
受保护的空onDestroy(){
super.ondestory();
checker.ondestory();
}
//授权回调类
私有类回调实现LicenseCheckerCallback{
公共无效允许(整数原因){
if(isFinishing()){
返回;
}
显示结果(“授权访问”);
}
公共无效dontAllow(内部原因){
if(isFinishing()){
返回;
}
显示结果(“访问被拒绝”);
如果(原因==策略。重试){
showDialog(对话框\重试);
}否则{
showDialog(对话框错误);
}
}
公共无效应用程序错误(int)
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();