我无法检测用户是否点击了安装Android应用程序后通常弹出的对话框。这样我就可以进行下一次安装或活动了。
答案 0 :(得分:2)
你无法检测到。
如果您想在第一次运行应用程序时执行某些操作,则只需在用户首选项中存储“首次运行”标志,并将其默认为true。
然后,您可以在应用程序启动时进行检查并执行任何必要的操作。
一些示例代码;
private boolean prefFirstRun;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = PreferenceManager.getDefaultSharedPreferences(this);
prefFirstRun= settings.getBoolean("FirstRun", true);
}
@Override
protected void onStart() {
super.onStart();
if (prefFirstRun) {
prefFirstRun = false;
// Do your initial operations here
}
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("FirstRun", false);
editor.commit();
}
答案 1 :(得分:1)
如果我找到你的话,
Open
还是Done
。这可以做到。为此,请像这样启动软件包安装程序。
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(new File("Full path to your APK")),
"application/vnd.android.package-archive"); // the APK path can be in SDCARD or in your application directory. I am sure you know this path.
// start the package-installer activity and wait for result. The second parameter can be used to identify the source of result in `onActivityResult` method.
startActivityForResult(intent, 1);
Open
或Done
按钮。onActivityResult
回调函数这里有onActivityResult
函数:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// requestCode == 1 means the result for package-installer activity
if (requestCode == 1)
{
// resultCode == RESULT_CANCELED means user pressed `Done` button
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "User pressed 'Done' button", Toast.LENGTH_SHORT).show();
}
else if (resultCode == RESULT_OK) {
// resultCode == RESULT_OK means user pressed `Open` button
Toast.makeText(this, "User pressed 'Open' button", Toast.LENGTH_SHORT).show();
}
}
super.onActivityResult(requestCode, resultCode, data);
}