How to launch an Android app without “android.intent.category.LAUNCHER”

前端 未结 4 1100
臣服心动
臣服心动 2020-12-05 21:22

I want to know how to launch an Android app without:

android.intent.category.LAUNCHER

Here is my AndroidManif

相关标签:
4条回答
  • 2020-12-05 21:50

    I want to know how to launch an Android app without: android.intent.category.LAUNCHER

    In Android, an Activity should be started by an Intent. Intents can start an Activity with startActivity(Intent myIntent), example:

    Intent myIntent= new Intent(context, target_class.class);
                        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(myIntent);
    

    You must replace "myIntent", "context" (usually a "this") and the "target_class" with your variables, in order for this to work. So, whenever you need your activity, call the Intent, ans the OS will resolve for the Activity

    0 讨论(0)
  • 2020-12-05 21:51

    You'll need to use a BroadcastReceiver.

    public class SafetyReceiver extends BroadcastReceiver {
    
        @Override
        public void onReceive(Context context, Intent intent) {
            ActivityManager as = (ActivityManager) context
                    .getSystemService(Activity.ACTIVITY_SERVICE);
            if (IsNavigationRunning(as)) {
                Intent i = new Intent(context, VoiceActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i);
            }
        }
    }
    

    Manifest:

    <application
        android:icon="@drawable/fot"
        android:label="@string/app_name" >
        <activity
            android:name="com.Safety.VoiceActivity"
            android:launchMode="singleTop"
            android:theme="@style/Theme.CompletelyTransparentWindow" >
        </activity>
        <receiver
            android:name="com.Safety.SafetyReceiver"
            android:process=":goodprocess" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
    </application>
    

    This is an example that starts when a text is received.

    0 讨论(0)
  • 2020-12-05 22:02

    This would be better if you want to launch a custom activity, for example on a button press:

    Button btn = (Button)findViewById(R.id.xBtn);
    
    btn.setOnClickListener(new OnClickListener() {
    @Override
                public void onClick(View v)
                {
                        Intent intent = new Intent("it.prassel.vimsmediaplayer");
                        intent.setComponent(ComponentName
                            .unflattenFromString("it.prassel.vimsmediaplayer/it.prassel.vimsmediaplayer.MainActivity"));
                        intent.addCategory("android.intent.category.LAUNCHER");
                        startActivity(intent);
                 }
    });
    

    And this is the manifest associated with your custom activity:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="it.prassel.vimsmediaplayer"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
    
    
        <uses-feature android:glEsVersion="0x00020000" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="it.prassel.vimsmediaplayer.MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="it.prassel.vimsmediaplayer" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    

    Where

    Intent intent = new Intent("it.prassel.vimsmediaplayer");
    

    corresponds to the line

    <action android:name="it.prassel.vimsmediaplayer" />
    

    And

    intent.setComponent(ComponentName
                            .unflattenFromString("it.prassel.vimsmediaplayer/it.prassel.vimsmediaplayer.MainActivity"))
    

    corresponds to the 2 lines

    package="it.prassel.vimsmediaplayer"  the part before /
    android:name="it.prassel.vimsmediaplayer.MainActivity" the part after /
    

    Your custom activity will not appear neither in the Menu nor in the Recent Apps, it will be completely anonymous. Pay attention, if you wish to be the only one who can acced to your app, to give the intent-filter a meaningful name

    0 讨论(0)
  • 2020-12-05 22:04

    you can specify in your class. For example on clicking of a button you want to go to some activity. so just write this code:

    Button btn = (Button)findViewById(R.id.xBtn);
    
    btn.setOnClickListener(new OnClickListener() {
    
             public void onClick(View v) {
              Intent intnt = new Intent();
              intnt.setAction(android.intent.action.MAIN);
              intnt.setCategory(android.intent.category.LAUNCHER);
              startActivity(intnt);
             }
            });
    
    0 讨论(0)
提交回复
热议问题