从应用程序首选项而不是从AndroidManifest.xml读取信息
我是Android开发新手,正在使用Eclipse开发一个Android应用程序。我提供了在Dropbox上同步数据库的功能。为此,Dropbox给我一个用于身份验证的键值。此密钥必须插入到AndroidManifest.xml中从应用程序首选项而不是从AndroidManifest.xml读取信息,android,dropbox,Android,Dropbox,我是Android开发新手,正在使用Eclipse开发一个Android应用程序。我提供了在Dropbox上同步数据库的功能。为此,Dropbox给我一个用于身份验证的键值。此密钥必须插入到AndroidManifest.xml中 <activity android:name="com.dropbox.client2.android.AuthActivity" android:configChanges="orientation|keyboard" android:launchMode="
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask" >
<intent-filter>
<!-- Change this to be db- followed by your app key -->
<data android:scheme="db-xxxxxxxxxx" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
但使用此逻辑,最终用户将无法更改此值以在其dropbox帐户(而不是我的帐户)上同步数据库。我已经制作了一个首选项屏幕来存储应用程序首选项中的键,但是我没有找到从Android清单读取值的代码。我想是关于这里的,但我是新手,不知道如何编辑我的代码:
public void startAuthentication(Context context) {
AppKeyPair appKeyPair = getAppKeyPair();
// Check if the app has set up its manifest properly.
Intent testIntent = new Intent(Intent.ACTION_VIEW);
String scheme = "db-" + appKeyPair.key;
String uri = scheme + "://" + AuthActivity.AUTH_VERSION + "/test";
testIntent.setData(Uri.parse(uri));
PackageManager pm = context.getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(testIntent, 0);
if (0 == activities.size()) {
throw new IllegalStateException("URI scheme in your app's " +
"manifest is not set up correctly. You should have a " +
"com.dropbox.client2.android.AuthActivity with the " +
"scheme: " + scheme);
} else if (activities.size() > 1) {
// Check to make sure there's no other app with this scheme.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Security alert");
builder.setMessage("Another app on your phone may be trying to " +
"pose as the app you are currently using. The malicious " +
"app cannot access your account, but linking to Dropbox " +
"has been disabled as a precaution. Please contact " +
"support@dropbox.com.");
builder.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
return;
} else {
// Just one activity registered for the URI scheme. Now make sure
// it's within the same package so when we return from web auth
// we're going back to this app and not some other app.
String authPackage = activities.get(0).activityInfo.packageName;
if (!context.getPackageName().equals(authPackage)) {
throw new IllegalStateException("There must be an " +
"AuthActivity within your app's package registered " +
"for your URI scheme (" + scheme + "). However, it " +
"appears that an activity in a different package is " +
"registered for that scheme instead. If you have " +
"multiple apps that all want to use the same access" +
"token pair, designate one of them to do " +
"authentication and have the other apps launch it " +
"and then retrieve the token pair from it.");
}
}
// Start Dropbox auth activity.
Intent intent = new Intent(context, AuthActivity.class);
intent.putExtra(AuthActivity.EXTRA_INTERNAL_CONSUMER_KEY,
appKeyPair.key);
intent.putExtra(AuthActivity.EXTRA_INTERNAL_CONSUMER_SECRET,
appKeyPair.secret);
if (!(context instanceof Activity)) {
// If starting the intent outside of an Activity, must include
// this. See startActivity(). Otherwise, we prefer to stay in
// the same task.
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
context.startActivity(intent);
public void start身份验证(上下文){
AppKeyPair AppKeyPair=getAppKeyPair();
//检查应用程序是否已正确设置其清单。
Intent testIntent=新的意图(Intent.ACTION\u视图);
字符串scheme=“db-”+appKeyPair.key;
字符串uri=scheme+“:/”+AuthActivity.AUTH_VERSION+“/test”;
setData(Uri.parse(Uri));
PackageManager pm=context.getPackageManager();
列表活动=pm.queryIntentActivities(testIntent,0);
如果(0==activities.size()){
抛出新的IllegalStateException(“应用程序中的URI方案”)+
“清单设置不正确。您应该有一个”+
“com.dropbox.client2.android.AuthActivity与”+
“方案:”+方案);
}else if(activities.size()>1){
//检查以确保没有其他应用程序使用此方案。
AlertDialog.Builder=新建AlertDialog.Builder(上下文);
builder.setTitle(“安全警报”);
builder.setMessage(“您手机上的另一个应用程序可能正在尝试”+
“冒充您当前使用的应用。恶意”+
应用程序无法访问您的帐户,但正在链接到Dropbox+
“作为预防措施,已禁用。请联系”+
"support@dropbox.com.");
setPositiveButton(“确定”,新的OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
});
builder.show();
返回;
}否则{
//仅为URI方案注册了一个活动。现在请确保
//它在同一个包中,所以当我们从web auth返回时
//我们将返回此应用程序,而不是其他应用程序。
字符串authPackage=activities.get(0).activityInfo.packageName;
如果(!context.getPackageName().equals(authPackage)){
抛出新的非法状态异常(“必须存在”+
“已注册应用程序包中的AuthActivity”+
对于您的URI方案(“+scheme+”)。但是+
“显示不同程序包中的活动为”+
“改为注册该计划。如果您有”+
“多个应用程序都希望使用相同的访问权限”+
令牌对,指定其中一个要执行的操作+
“身份验证并让其他应用启动它”+
“然后从中检索令牌对。”);
}
}
//启动Dropbox验证活动。
意向意向=新意向(上下文,AuthActivity.class);
intent.putExtra(AuthActivity.EXTRA\u内部\u使用者\u键,
appKeyPair.key);
intent.putExtra(AuthActivity.EXTRA\u INTERNAL\u CONSUMER\u SECRET,
appKeyPair.secret);
if(!(活动的上下文实例)){
//如果在活动之外启动意图,则必须包括
//这是。请参见startActivity()。否则,我们宁愿留在
//同样的任务。
intent.addFlags(intent.FLAG\u活动\u新任务);
}
背景。开始触觉(意图);
你能帮我吗
提前感谢您对象appKeyPair.key包含您想要的密钥。
我不知道AppKeyPair AppKeyPair=getAppKeyPair();做了什么,但我认为您可以删除它并从共享首选项中放入一个简单字符串,而不是调用
AppKeyPair.key
对象AppKeyPair.key包含您想要的密钥。
我不知道AppKeyPair AppKeyPair=getAppKeyPair();有什么作用,但我认为您可以将其删除并从共享首选项中放入一个简单字符串,而不是调用
AppKeyPair.key
我认为您可能误解了该密钥的用途。它不用于选择与哪个帐户同步文件
您要更改的密钥是应用程序API密钥。每个用户不需要唯一的密钥,除非您针对的是已经拥有自己API密钥的开发人员。如果应用程序给Dropbox带来麻烦,这可用于识别应用程序并将其关闭
您尝试编辑的此密钥的特定实例是在用户使用其个人帐户进行身份验证后将控制权返回到您的应用程序的实例。这需要与您在调用AppKeyPair appKeys=new AppKeyPair(app_key,app_SECRET)时用于身份验证的密钥保持同步
您将无法在运行时修改该意图筛选器中的数据标记。我认为您可能误解了该密钥的用途。它不用于选择与哪个帐户同步文件 您要更改的密钥是应用程序API密钥。每个用户不需要唯一的密钥,除非您针对的是已经拥有自己API密钥的开发人员。如果应用程序给Dropbox带来麻烦,这可用于识别应用程序并将其关闭 您正试图编辑的此密钥的特定实例是在用户使用其个人帐户进行身份验证后将控制权返回到您的应用程序的实例。这需要与您用于authen的密钥保持同步