在android应用程序的后台服务中强制关闭

发布于 2024-12-26 15:26:43 字数 1335 浏览 2 评论 0原文

你好。我按照本教程创建了一个后台服务应用程序 http://androidsourcecode .blogspot.com/2010/10/basic-android-background-service.html。但我的清单文件有错误。

错误消息

The element type "category" must be terminated by the matching end-tag "</category>".

我不知道如何创建 Android 后台服务。对此的任何指导表示赞赏。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.javaorigin.android.sample.service" android:versionCode="1"
 android:versionName="1.0">
  <application icon="@drawable/icon" label="@string/app_name">
   <service class=".MyService" name=".MyService">
     <intent-filter>
       <action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
               android:name=".MyService" />

       </intent-filter>
   </service>
  <activity android:name=".SampleAction"
             android:label="@string/app_name">
       <intent-filter>
           <action name="android.intent.action.MAIN">
           <category name="android.intent.category.LAUNCHER">
       </intent-filter>
   </activity>

Hi. I have create a background service application following this tutorial http://androidsourcecode.blogspot.com/2010/10/basic-android-background-service.html. But I have an error in the manifest file.

Error Message

The element type "category" must be terminated by the matching end-tag "</category>".

I don't know how to create an Android background service. Any guidance on that is appreciated.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.javaorigin.android.sample.service" android:versionCode="1"
 android:versionName="1.0">
  <application icon="@drawable/icon" label="@string/app_name">
   <service class=".MyService" name=".MyService">
     <intent-filter>
       <action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
               android:name=".MyService" />

       </intent-filter>
   </service>
  <activity android:name=".SampleAction"
             android:label="@string/app_name">
       <intent-filter>
           <action name="android.intent.action.MAIN">
           <category name="android.intent.category.LAUNCHER">
       </intent-filter>
   </activity>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄荷→糖丶微凉 2025-01-02 15:26:43

您的清单文件没有大问题。该教程看起来很旧,您需要做一些更改才能消除错误。就像使用正斜杠结束操作和类别标签一样,不要使用 android:name 属性名称,而是将 use-sdk 移到应用程序标签之前(Lint 更喜欢这样做)。就是这样 !

试试这个:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.javaorigin.android.sample.service" android:versionCode="1"
   android:versionName="1.0">
   <uses-sdk minsdkversion="8"/>
   <application icon="@drawable/icon" label="@string/app_name">
       <service class=".MyService" android:name=".MyService">
         <intent-filter>
           <action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
                   android:name=".MyService" />

           </intent-filter>
       </service>
      <activity android:name=".SampleAction"
                 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>

There is no big problem in your manifest file. That tutorial seems like old and you need to do few changes to get rid of errors. Like the forward slashes to end action and category tag, instead of attribute name use android:name, move the uses-sdk before application tag(where Lint prefer it). Thats it !

Try This :

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.javaorigin.android.sample.service" android:versionCode="1"
   android:versionName="1.0">
   <uses-sdk minsdkversion="8"/>
   <application icon="@drawable/icon" label="@string/app_name">
       <service class=".MyService" android:name=".MyService">
         <intent-filter>
           <action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
                   android:name=".MyService" />

           </intent-filter>
       </service>
      <activity android:name=".SampleAction"
                 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>
你的往事 2025-01-02 15:26:43

它与 Service 元素无关。您的 XML 格式错误;任何不包含嵌套元素的标签必须在同一行上以斜杠终止。这是更正后的块:

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

注意 MAIN 和 LAUNCHER 之后的斜杠。如果没有这些,解析器会认为您将标签保持打开状态,以便在其下面嵌套子级。

It's not related to the Service element. Your XML is malformed; any tags that do not contain nested elements must terminate on the same line with a slash. Here is the corrected block:

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

Note the slashes after MAIN and LAUNCHER. Without these, the parser thinks you are leaving the tag open to nest children underneath it.

守望孤独 2025-01-02 15:26:43

将其复制粘贴到您的清单中:

  <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.javaorigin.android.sample.service" android:versionCode="1"
     android:versionName="1.0">
      <application icon="@drawable/icon" label="@string/app_name">
       <service class=".MyService" name=".MyService">
         <intent-filter>
           <action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
                   android:name=".MyService" />

           </intent-filter>
       </service>
      <activity android:name=".SampleAction"
                 android:label="@string/app_name">
           <intent-filter>
               <action name="android.intent.action.MAIN"/>
               <category name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
     </application>
    </manifest>

Copy paste this in your manifest:

  <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.javaorigin.android.sample.service" android:versionCode="1"
     android:versionName="1.0">
      <application icon="@drawable/icon" label="@string/app_name">
       <service class=".MyService" name=".MyService">
         <intent-filter>
           <action android:value="com.javaorigin.android.sample.service.MY_SERVICE"
                   android:name=".MyService" />

           </intent-filter>
       </service>
      <activity android:name=".SampleAction"
                 android:label="@string/app_name">
           <intent-filter>
               <action name="android.intent.action.MAIN"/>
               <category name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
     </application>
    </manifest>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文