Android小部件是否有“onCreate”方法?

3
我正在按照这个教程(链接)为我的Android应用添加 EULA,但是我有两个问题: 1) 我的应用程序基于小部件,我想在小部件启动时立即显示我的EULA。 小部件是否有“ onCreate”方法? 2) 如果用户拒绝 EULA,则我想关闭我的应用程序。 我是C#程序员,因此不知道Android应用程序是否有“ Exit()”方法。 如何在没有用户操作的情况下强制关闭我的应用程序?
3个回答

5
根据安卓应用小部件文档页面,您可能会对onEnabledonDisabled方法感兴趣。
onEnabled(Context)
This is called when an instance the App Widget is created for the first time. 
For example, if the user adds two instances of your App Widget, this is only called the first time. 
If you need to open a new database or perform other setup that only needs to occur once for all App Widget instances, then this is a good place to do it.

onDisabled(Context)
This is called when the last instance of your App Widget is deleted from the App Widget host. 
This is where you should clean up any work done in onEnabled(Context), such as delete a temporary database.

如果用户拒绝,则可以调用onDisabled(Context)函数。


1

没有一个明确的onCreate()方法,但是有一种方法可以在小部件首次添加时显示一个活动。一种做法如下:

在你的AppWidget提供程序XML文件中,确保将其作为appwidget-provider的属性添加:

android:configure="your.eula.activity"

不要忘记在你的AndroidManifest.xml中声明your.eula.activity

<activity android:name="your.eula.activity">
     <intent-filter>
           <action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
     </intent-filter>
</activity>

在你的EULA活动中的onCreate()方法中,你应该调用your.eula.activity
 setResult(RESULT_CANCELED);
 finish();

无法工作 - Eula类不是一个活动,它只是一个类 :) - undefined
@guess86: 为什么你不能将Eula类作为一个活动呢? - undefined
是的,我现在正在处理这个问题,然后我会尝试上面帖子中的代码。 - undefined
只是好奇,因为我实际上没有测试过这个方法——我的方法有效吗? - undefined

0

你不能在初始化和其他操作时显示最终用户许可协议吗?我对你的代码不熟悉,所以不确定在你的情况下是否可能,但这是一个可能性。

要结束一个活动,只需调用this.finish()


不仅仅是活动 - 整个应用程序!如何实现这一点? - undefined
我真的不认为这是推荐的做法。但是按照以下方法应该可以解决问题:android.os.Process.killProcess(android.os.Process.myPid()); - undefined
为什么你需要真正终止进程呢?如果有人想要这样做,他们可以拒绝你的最终用户许可协议(EULA),然后手动启动你应用中的任何活动。你可以将EULA接受状态写入SharedPreferences,并在你应用的所有活动和组件中设置一个守卫标志。即使如此,这也可以被规避,对我来说似乎没有什么实际意义。 - undefined
有没有办法强制用户选择:接受最终用户许可协议并使用程序,或者拒绝许可协议并且除非他/她接受协议否则无法使用? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接