简体   繁体   English

广播接收器未启动活动

[英]BroadCast Receiver not launching activity

I am implemeting a hidden app.我正在实现一个隐藏的应用程序。 I wantt to hide the app in the phone and launch it when I call a certain number.我想隐藏手机中的应用程序并在我拨打某个号码时启动它。

In first place I have an alias-activity declared on the manifest, and I hide it.首先,我在清单上声明了一个别名活动,并将其隐藏。 It is MainActivity2:它是 MainActivity2:

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    PackageManager p = getPackageManager();
    ComponentName LAUNCHER_COMPONENT_NAME = new ComponentName(
            "com.example.susan.oculta.launcher", "com.example.susan.oculta.launcher.Launcher");
    p.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);...

Some Manifest lines:一些清单行:

<activity-alias
        android:name=".launcher.Launcher"
        android:targetActivity=".launcher.Main2Activity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity-alias>

With this code I am able to hide the icon.使用此代码,我可以隐藏图标。

I also have a BroadCast Receiver.我也有一个广播接收器。 This one:这个:

public class LaunchAppViaDialReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    if (null == bundle)
        return;
    String phoneNubmer = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    //here change the number to your desired number
    if (phoneNubmer.equals("12345")) {
        Log.i("reciving", "receeeeeeeeeiving");

        Intent i = new Intent();
        i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.setClass(context, MainActivity.class);
        context.startActivity(i);

        setResultData(null);
        Toast.makeText(context, "aaaa", Toast.LENGTH_LONG).show();

    }
}

} }

The problem is that when I dial "12345" the Toast shows up, but MainActivity does not.问题是,当我拨打“12345”时,Toast 出现了,但 MainActivity 没有。 I have tried adding flags to the intent and starting the intent finding it by package.我已经尝试向意图添加标志并开始通过包查找它的意图。 But the intent seems to not be working.但意图似乎不起作用。 The log "receeeeeeeeeiving" also shows up.日志“receeeeeeeeiving”也出现了。

Try this 尝试这个

if (phoneNubmer.equals("12345")) {

     Log.i("reciving", "receeeeeeeeeiving"); 
     Intent i = new Intent(context, MainActivity.class);
     i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
     context.startActivity(i);
     Toast.makeText(context, "aaaa", Toast.LENGTH_LONG).show();
}

Try this: 尝试这个:

Intent i = new Intent();
String packageName = context.getPackageName();
ComponentName componentName = new 
ComponentName(packageName, packageName + "your_ALIAS_ACTIVITY");
i.setComponent(componentName);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);

When an app is initially installed, it's in the "stopped" state. 最初安装应用程序时,它处于“已停止”状态。 It remains in the stopped state until the user has explicitly launched one of its activities. 它保持在停止状态,直到用户明确启动其活动之一。 If the user force closes the app, it is returned to the stopped state. 如果用户强行关闭应用程序,则它将返回到停止状态。

When an app is in the stopped state, none of its broadcast receivers will ever be called. 当应用程序处于停止状态时,将不会调用任何广播接收器。 I suspect that the only reason your BroadcastReceiver is being called at all is because you launched your main activity at some point during development. 我怀疑根本无法调用BroadcastReceiver的唯一原因是因为您在开发过程中的某个时候启动了主要活动。 Now, being disabled, it won't run. 现在,被禁用,它将无法运行。 If you were to install this app on a fresh device (or force close it to reset it to the stopped state) you would no longer even see your log and toast. 如果您要将此应用安装在全新的设备上(或强行关闭该应用以将其重置为停止状态),您将什至看不到日志和吐司。

There are multiple solutions to this :对此有多种解决方案:

  • You give SYSTEM_ALERT_WINDOW in the Manifest, then you redirect the user (at runtime) to 'Draw over other apps' setting.您在清单中提供 SYSTEM_ALERT_WINDOW,然后将用户(在运行时)重定向到“绘制其他应用程序”设置。 This way, you can launch any background/killed activity on Android 10 or higher.这样,您就可以在 Android 10 或更高版本上启动任何后台/已终止的活动。

For those who have the same issue on MIUI OS phones (Xiaomi), you have multiple work arounds :对于那些在 MIUI OS 手机(小米)上遇到相同问题的人,您有多种解决方法:

  • SYSTEM_ALERT_WINDOW can't save you with Xiaomi phones. SYSTEM_ALERT_WINDOW 不能用小米手机救你。 Therefore, you need an AccessibilityService to bypass the MIUI limitation, otherwise, you can just set a Full-Screen High-Priority notification that will definitely launch activity if the phone is in "Idle" state.因此,您需要一个 AccessibilityService 来绕过 MIUI 限制,否则,您可以只设置一个全屏高优先级通知,如果手机处于“空闲”状态,它肯定会启动活动。 If the user is using the phone, the notification appears in the drawer and will only launch activity if the user clicks on it.如果用户正在使用手机,通知会出现在抽屉中,并且只有在用户点击它时才会启动活动。

These were all the non-root solutions.这些都是非root解决方案。

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

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