必须在每次活动都有一个布局?都有、布局

2023-09-12 05:11:51 作者:命由己造.

我刚开始使用Android和正在读了BroadcastReceiver的。由于正在只用了MainActivity取得秒的报警时间,这让我思考布局的XML文件是否必须为每一个Android的活动。我的意思是,是否有可能有一个应用程序在启动时,不显示视图,但成功地建立了一个接收器?

I'm just getting started with Android and was reading up BroadcastReceiver. Since the MainActivity was being used only to get the alarm time in seconds, it got me thinking whether layout XML files are must for every activity in Android. I mean, is it possible to have an app that when launched, shows no view, but successfully sets up a receiver?

推荐答案

答案是肯定的这是可能的。活动不必具有的UI。它的所提到的文件,例如在:

The answer is yes it's possible. Activities don't have to have a UI. It's mentioned in the documentation, e.g.:

这是活动是一个单一的,集中的一点是,用户可以做的。几乎   所有活动与用户交互[...]

An activity is a single, focused thing that the user can do. Almost all activities interact with the user [...]

(见 http://developer.android.com/reference/android/应用程序/ Activity.html )

相关SO问题: http://stackoverflow.com/a/12817384/534471

要如显示从活动敬酒不布局,你会定义活动您的清单如下所示:

To e.g. display a Toast from an Activity without layout you would define the activity in your manifest like so:

<activity
    android:name=".MainActivity"
    android:theme="@android:style/Theme.NoDisplay">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

在code是这样的:

The code would look like this:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(this, "I'm alive", Toast.LENGTH_LONG).show();
        finish();
    }
}