【问题标题】:Launch activity on Boot completed and another activity on Mount completionBoot 完成时的启动活动和 Mount 完成时的另一个活动
【发布时间】:2013-11-06 09:48:36
【问题描述】:

这是我的 Androidmanifest.xml

      <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abc.testapp"
  android:versionCode="1"
    android:versionName="Pm61" >
 <uses-sdk android:minSdkVersion="15"/>
  <uses-permission android:name="android.permission.BLUETOOTH"/>
  <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
  <supports-screens android:anyDensity="true" />

   <application android:label="@string/app_name" android:debuggable="true"   android:largeHeap="true">

       <activity android:name="com.abc.testapp.MainClass" android:label="@string/app_name" android:theme="@android:style/Theme.Translucent" android:hardwareAccelerated="true">
          <intent-filter>
           <action android:name="android.intent.action.MAIN"/>
           <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
           </activity>

             .
             .
             .
             .
             .
             .
            <activity android:name="com.abc.testapp.BootLoad"
             android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
          </activity>

          <activity android:name="com.abc.testapp.Rxmain"  android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:launchMode="singleTask">
         </activity> 

         <receiver  android:name="com.abc.testapp.MyReceiver" android:enabled="true">
          <intent-filter android:priority="500">
                <action android:name= "android.intent.action.BOOT_COMPLETED"/>
                <action       android:name="android.intent.action.MEDIA_MOUNTED"/> 
                <data android:scheme="file" /> 
          </intent-filter>
         </receiver>

    </application>  

  </manifest> 

这是我的 MyReceiver 广播类

      package com.abc.testapp;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.util.Log;

public class MyReceiver extends BroadcastReceiver 
  {

   @Override
   public void onReceive(Context context, Intent intent) 
       {
String action = intent.getAction();

  if(Intent.ACTION_MEDIA_MOUNTED.equals(action))
    {
      Log.d("MYReceiver","Mounting Successfull");
      Intent serviceActivity = new Intent(context, Rxmain.class);
      serviceActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(serviceActivity); 
    }

  if(Intent.ACTION_BOOT_COMPLETED.equals(action))
  {
      Log.d("MYReceiver","Boot Successfull");
      Intent serviceActivity = new Intent(context, BootLoad.class);
      serviceActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      context.startActivity(serviceActivity); 
  }
  }
}        

我的设备是独立设备,只有我的应用会启动。

我希望在启动完成时启动 BootLoad 活动,并在 MEDIA_MOUNTED 后启动 Rxmain Activity。

但是我的 bootLoad 活动没有到来

所以我对此有一些疑问:

  1. 它有时能工作,有时不能?

2.Intent-filter 中的优先级是什么?

  1. 这个数据方案有什么作用?

  2. 我所做的正确与否?

请给我建议

【问题讨论】:

    标签: android android-intent broadcastreceiver android-manifest


    【解决方案1】:

    问题在于您为广播接收器定义意图过滤器的方式。这是你的定义:

    <receiver  android:name="com.abc.testapp.MyReceiver" android:enabled="true">
        <intent-filter android:priority="500">
              <action android:name= "android.intent.action.BOOT_COMPLETED"/>
              <action       android:name="android.intent.action.MEDIA_MOUNTED"/> 
              <data android:scheme="file" /> 
        </intent-filter>
    </receiver>
    

    您已经定义了一个 Intent 过滤器,如果 ACTION 为 BOOT_COMPLETEDMEDIA_MOUNTED,则会触发该过滤器。但是,通过指定&lt;data&gt; 标签,您将只接收具有data with scheme=file的广播Intent。

    BOOT_COMPLETED Intent 没有任何数据,因此您的接收者不会得到它。

    您需要指定 2 个单独的意图过滤器,如下所示:

    <receiver  android:name="com.abc.testapp.MyReceiver" android:enabled="true">
        <intent-filter android:priority="500">
              <action android:name= "android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
        <intent-filter android:priority="500">
              <action       android:name="android.intent.action.MEDIA_MOUNTED"/> 
              <data android:scheme="file" /> 
        </intent-filter>
    </receiver>
    

    【讨论】:

    • 感谢您的回复,它可以正常工作,但我面临另一个问题 Rxmain Class Activity 被调用两次。如何避免这种情况
    • 为什么会被调用两次?你看到了什么广播意图?添加一些日志记录或查看 logcat
    • onreceiver Mounting Successfull 被调用了两次。即使我收到两个 MEDIA_MOUNTED 事件,如何再次阻止 Rxmain.java 类。如果 MEDIA_MOUNTED 的条件下一次它不应该出现。
    • 好吧,也许您需要跟踪您已经在共享首选项中处理了 MEDIA_MOUNTED 事件的事实。然后,下次来的时候,你可以检查一下,看看你是否已经处理了这个。不过,我不确定您的应用程序要求到底是什么。
    • 是这样的,我是 Android 新手。你能给我一些示例代码或编辑我的问题吗?
    猜你喜欢
    • 2013-03-25
    • 2016-03-18
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    相关资源
    最近更新 更多