Start Activity from BroadcastReceiver

vanKraster 201 Reputation points
2022-01-04T07:35:45.947+00:00

Hi to all,

I have to hide an app and open when somethings happen so i hide it in Android >> MainActivity I use this code

 PackageManager p = Android.App.Application.Context.PackageManager;// this.PackageManager; 
            ComponentName componentName = new ComponentName(this, Class);
            p.SetComponentEnabledSetting(componentName, ComponentEnabledState.Disabled, ComponentEnableOption.DontKillApp);

And in my App BroadcastReceiver I use this

public override void OnReceive(Context context, Intent intent)
    {
        unhideApp(context);
        Intent intent2 = new Intent(context, typeof(MainActivity));
        intent2.SetFlags(ActivityFlags.NewTask);


        context.StartActivity(intent2);
    }



    private void unhideApp(Context context)
    {
        PackageManager p = Android.App.Application.Context.PackageManager;// this.PackageManager; 

        var mainActivity = new MainActivity();
        ComponentName componentName = new ComponentName(context, Class);
        p.SetComponentEnabledSetting(componentName, ComponentEnabledState.Enabled, ComponentEnableOption.DontKillApp);

    }

It hides the app, the broadcast is working but I cannot open the app because when he reaches the line with : context.StartActivity(intent2);

it gives me an error something like that :
Unable to find explicit activity class {MainActivity} have you declared this activity in your AndroidManifest.xml

I have tryed to add this in my androidmanifest.xml but it doesn't work

  <activity
             android:name=".MainActivity"
             android:label="SmartShopInventario" >
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>

Can you help me please...

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. JarvanZhang 23,971 Reputation points
    2022-01-05T03:51:08.903+00:00

    Hello vanKraster-4073,​

    Welcome to our Microsoft Q&A platform!

    it gives me an error something like that : Unable to find explicit activity class {MainActivity} have you declared this activity in your AndroidManifest.xml

    This is because the MainActivity class has been disabled and it's not eabled before calling StartActivity command. It requires to specify the MainActivity Class as the parameter explicitly.

    Here is the sample code, you could refer to it.

       [BroadcastReceiver]  
       public class TestReceiver : BroadcastReceiver  
       {  
           public override void OnReceive(Context context, Intent intent)  
           {  
               PackageManager p = Android.App.Application.Context.PackageManager;// this.PackageManager;   
               ComponentName componentName = new ComponentName(context, MainActivity.Instance.Class);  
               p.SetComponentEnabledSetting(componentName, ComponentEnabledState.Enabled, ComponentEnableOption.DontKillApp);  
         
               Intent i = new Intent(context, typeof(MainActivity));  
               i.SetFlags(ActivityFlags.NewTask);  
               context.StartActivity(i);  
           }  
       }  
         
       public class MainActivity : AppCompatActivity  
       {  
           public static MainActivity Instance { get; set; }  
           protected override void OnCreate(Bundle savedInstanceState)  
           {  
               ...  
               Instance = this;  
           }  
       }  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.