我刚接触Android和Javascript。但是,我正在通过观看教程和提问来积极学习。现在,我正在尝试编写更改某些系统值的应用程序。我知道这只能通过向我的应用授予WRITE_SECURE_SETTINGS权限来完成。通过研究,我意识到我无法在清单文件本身中声明许可。显然,唯一可取的答案是检查用户是否已通过ADB授予了许可。我找不到与此主题相关的任何内容,希望你们能帮助我得出有关如何正确执行此操作的结论。到目前为止,这是我拥有的代码。
package com.datastream.settingschanger;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String requiredPermission = "android.permission.WRITE_SECURE_SETTINGS";
int checkVal = getContext().checkCallingOrSelfPermission(requiredPermission);
if (checkVal == PackageManager.PERMISSION_GRANTED) {
}
Toast.makeText(getActivity(), "WRITE_SECURE_SETTINGS has been granted to the application. You may now continue!",
Toast.LENGTH_LONG).show();
if (checkVal == PackageManager.PERMISSION_DENIED) {
}
Toast.makeText(getActivity(), "WRITE_SECURE_SETTINGS has not been granted to the application. Please grant access to continue.",
Toast.LENGTH_LONG).show();
}
}
但是,当我在Android Studio中运行此程序时,会出现以下错误:
error: cannot find symbol method getContext()
error: cannot find symbol method getActivity()
error: cannot find symbol method getActivity()
有人可以帮助我解决这些错误吗?我不知道如何解决这个问题。谢谢!
答案 0 :(得分:2)
getContext()
中使用getActivity()
和Fragment
方法来获取{{1 }}
删除 Context
和 getContext()
并使用 getActivity()
和 {{1 }} 获得this
的活动
使用此
MainActivity.this
代替此
Context
示例代码
int checkVal = checkCallingOrSelfPermission(requiredPermission);