简体   繁体   English

如何在清单文件中注册应用程序类?

[英]How to register application class in manifest file?

How to register my application class in my android manifest? 如何在我的Android清单中注册我的应用程序类? I have looked at many tutorials online and still can not get it right. 我在网上看了很多教程,但仍然无法做到正确。 My application class is called Monitor.java . 我的应用程序类叫做Monitor.java How do I register it in the Manifest file code below? 如何在下面的清单文件代码中注册它?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.project"
    android:versionCode="1"
    android:versionName="1.0" >

<application
    android:allowBackup="true"
    android:debuggable="true"
    android:icon="@drawable/ic_launcher"
    android:label="xyz"
    android:screenOrientation="landscape"
    android:theme="@style/AppTheme" >

    <service
        android:name=".AudioService"
        android:icon="@drawable/ic_launcher"
        android:label="audioservice" >
    </service>

    <activity
        android:name=".MainActivity"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".Editor"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="stateHidden" >
        <intent-filter>
            <action android:name="com.example.project.EDITOR" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

<application
        android:name="package.YourApplicationClass"  <--------
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@drawable/ic_launcher"
        android:label="xyz"
        android:screenOrientation="landscape"
        android:theme="@style/AppTheme">

Try this: 尝试这个:

 <application android:icon="@drawable/icon" 
       android:label="@string/app_name" 
       android:name="Monitor">

See the good reference link below : 请参阅下面的参考链接:

How to use the Application object of Android 如何使用Android的Application对象

Thanks. 谢谢。

Simple, put android:name attribute with your application class name in <application /> tag of Application's Manifest.xml 很简单,将android:name属性与应用程序类名称放在Application的Manifest.xml的 <application />标签中

<application
 android:name=".Monitor"
   .... >

Update: 更新:

Application Class: 申请类别:

Base class for those who need to maintain global application state. 需要维护全局应用程序状态的基类。 You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created. 您可以通过在AndroidManifest.xml的标记中指定其名称来提供自己的实现,这将导致在创建应用程序/包的进程时为您实例化该类。

More info look at http://developer.android.com/reference/android/app/Application.html 更多信息请查看http://developer.android.com/reference/android/app/Application.html

Well, you already have application class inside - starting with <application . 好吧,你已经有了应用程序类 - 从<application开始。 If you have a custom class (that extends Application ) in your code and want it to start - put 如果您的代码中有自定义类(扩展Application )并希望它开始 - 放置

android:name=".Monitor" (or full path like com.something.app.Monitor)

after <application tag (the same process as you would add an activity). <application tag之后(与添加活动的过程相同)。

Just add an android:name=".Monitor" attribute to the application tag (I presume that the Monitor.java class is located in the root of the application package). 只需在application标记中添加一个android:name=".Monitor"属性(我假设Monitor.java类位于应用程序包的根目录中)。 Hope this helps. 希望这可以帮助。

what confused me is 'android:name' shows up multiple times in the manifest file. 令我困惑的是'android:name'在清单文件中多次显示。 before creating an application class file, i had 在创建应用程序类文件之前,我有

android:name=".MainActivity"

after the 'activity' tag. 在'活动'标签之后。

after creating the application file, everything in manifest file stays the same, except after 'application' tag, i add 创建应用程序文件后,清单文件中的所有内容都保持不变,除了'application'标记后,我添加

android:name=".myApplicationClass"

my complete manifest file: 我的完整清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pixtas.com.nightout" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <!--
      IMPORTANT: Change "com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE" in the lines below
      to match your app's package name + ".permission.C2D_MESSAGE".
    -->
    <permission android:protectionLevel="signature"
        android:name="pixtas.com.nightout.permission.C2D_MESSAGE" />
    <uses-permission android:name="pixtas.com.nightout.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name=".myApplicationClass" >


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.parse.PushService" />
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <!--
                  IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name.
                -->
                <category android:name="pixtas.com.nightout" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.parse.ParsePushBroadcastReceiver" android:exported="false">
            <intent-filter>
                <action android:name="com.parse.push.intent.RECEIVE" />
                <action android:name="com.parse.push.intent.DELETE" />
                <action android:name="com.parse.push.intent.OPEN" />
            </intent-filter>
        </receiver>

        <!-- replace @drawable/push_icon with your push icon identifier -->
        <meta-data android:name="com.parse.push.notification_icon" android:resource="@drawable/ic_launcher"/>

    </application>

</manifest>

use this one 用这个

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testing"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Monitor"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM