有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在启动时启动服务,并从BR的onReceive方法启动活动

首先,我不想说我试图在stackoverflow上找到答案,但没有成功。其次,我在三星S34G 4.1.2上测试了我的代码

我对自己编写的代码有些疑问

——解释:
代码在BOOT_COMPLETED时启动ClsAutoStart,启动服务SvcFirst,该服务注册一个与NEW_OUTGOING_CALL动作同步的BroadcastReceiver。在onReceive()方法中,我启动活动

您知道,活动应该在呼叫时弹出(目前还没有条件)

——问题:
这是可行的,但我没有准确的预期行为,因此我有一些问题:

1-只有在安装后和重新启动前先打开活动,一切才能顺利进行。然后,在那之后,每段代码都会做它必须做的事情。有人能解释一下为什么我要启动这个活动才能让它工作吗?如何避免

2-我之所以使用这种方法做事情,是因为我不希望我的应用程序出现在应用程序列表中(对于用户)。我不想通过onReceive方法启动活动。 我试图从清单文件中删除以下几行,但我回到了问题“1”:如果我之前不能启动活动,它就不起作用(因为这些行已被删除,所以不可能启动任何东西)

<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />

有人能解释一下我为什么不工作,以及如何纠正吗
这是“隐藏”我的应用程序的正确方法吗
在stackoverflow上,我找到了很多答案。。。这是其中之一(问题“3”是其他问题之一…)

3-我删除了前面的行(问题“2),并在清单文件的接收方部分(BOOT_COMPLETED action)添加了下面的一行,但它仍然没有改变任何东西。有什么线索吗

<category 安卓:name="安卓.intent.category.HOME" />

——代码:

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="com.frontal.invisible04"
    安卓:versionCode="1"
    安卓:versionName="1.0" >

    <uses-sdk
        安卓:minSdkVersion="8"
        安卓:targetSdkVersion="19" />

    <uses-permission 安卓:name="安卓.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission 安卓:name="安卓.permission.PROCESS_OUTGOING_CALLS" />

    <application
        安卓:allowBackup="true"
        安卓:icon="@drawable/ic_launcher"
        安卓:label="@string/app_name"
        安卓:theme="@style/AppTheme" >
        <activity
            安卓:name="com.frontal.invisible04.ActMain"
            安卓:label="@string/title_activity_act_main" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />
                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service
            安卓:name="com.frontal.invisible04.SvcFirst">
        </service>

        <receiver 
            安卓:name="com.frontal.invisible04.ClsAutoStart">
            <intent-filter>
                <action 安卓:name="安卓.intent.action.BOOT_COMPLETED" />
                <!--<category 安卓:name="安卓.intent.category.HOME" />-->
            </intent-filter>
        </receiver>        
    </application>
</manifest>

ClsAutoStart:

public class ClsAutoStart extends BroadcastReceiver 
{
 @Override
 public void onReceive(Context context, Intent intent) 
  {    
   if ((intent.getAction() != null) && (intent.getAction().equals("安卓.intent.action.BOOT_COMPLETED")))
    {
     context.startService(new Intent(context, SvcFirst.class));      
    }         
  }
}

SvcFirst:

public class SvcFirst extends Service
{
 Context context;
 private BroadcastReceiver br_interface;
 private static final String ACTION_SHOWACT = "安卓.intent.action.NEW_OUTGOING_CALL";

 @Override
 public void onCreate()
  {
   super.onCreate();   
   this.context = getApplicationContext(); 
  }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId)
  {
   final IntentFilter filter = new IntentFilter();
   filter.addAction(ACTION_SHOWACT);   
   this.br_interface = new SecretInterfaceBR();
   this.registerReceiver(this.br_interface, filter);
   return (START_STICKY);
  } 

 public class SecretInterfaceBR extends BroadcastReceiver 
  {
   @Override
   public void onReceive(Context context, Intent intent) 
    {
     Intent intent1 = new Intent(context, ActMain.class);
     intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
     context.startActivity(intent1);
    }
  }

 @Override
 public IBinder onBind(Intent intent) 
  { 
   return (null);
  } 

 public void onDestroy()
  {
   this.unregisterReceiver(this.br_interface); 
  }
}

谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    Could anyone explain me why I have to launch the activity in order it to work ?

    因为从Android 3.1开始,当应用程序处于强制停止状态时,清单注册的接收者将无法工作。出现这种情况:

    • 第一次安装时

    • 用户点击设置中的“强制停止”按钮后

    How to avoid it ?

    仅支持Android 3.0及以下版本。请注意,这是Android设备市场的一个递减百分比

    Could anyone explain me why I doesn't work and how to correct it ?

    见上图

    Is this the correct way to do "hide" my application ?

    您所描述的并不是隐藏一个应用程序,而只是表示一个没有启动程序图标的应用程序。由于前面提到的强制停止限制,从Android 3.1开始,此类应用程序通常不再有用。而且,这些应用程序仍然在其他选择列出应用程序的地方列出,比如设置应用程序

    I removed the previous lines (question "2) and add the following one in the receiver section (of BOOT_COMPLETED action) in my manifest file but it still doesn't change anything. Any clue ?

    接收器很少使用<category>HOME类别与活动一起使用,并指定主屏幕实现