简体   繁体   中英

App just for launching an activity

I'm trying to add a shortcut to launch Sound Recorder on App Drawer (not home screen)

So the app should be empty and the only work it has to do is launch Sound Recorder com.android.soundrecorder/com.android.soundrecorder.SoundRecorder

How can I do that?

I get Force Stop with this:

`

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Recorder"
        android:label="@string/title_activity_recorder" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

`

and the java file

`package recorder.audio.dsaif;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class Recorder extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.soundrecorder.SoundRecorder");
    startActivity(LaunchIntent);
    setContentView(R.layout.activity_recorder);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_recorder, menu);
    return true;

}
}`

Two ways:

Create your own app. From there launching a different app is very simple.

Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.soundrecorder.SoundRecorder");
startActivity(LaunchIntent);

OR

Download Tasker. It has a feature to let you create apps right from your phone. You can simply make a Tasker profile that launches the app you want.

i think you need to add a flag of a new task , and also call finish() and remove the setContentView.

if the crash is caused by a null pointer exception , maybe the package is incorrect and/or the app isn't installed.

that's why you need to check if the intent was given to you or null.

btw, variables names should have a lowercase letter.

code of the activity here:

@Override
  protected void onCreate(final Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    final Intent launchIntent=getPackageManager().getLaunchIntentForPackage("com.android.soundrecorder.SoundRecorder");
    if(launchIntent==null)
      {
      Toast.makeText(this,"can't find the app to launch!",Toast.LENGTH_SHORT).show();
      finish();
      return;
      }
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(launchIntent);
    finish();
    }

in the manifest file , add this attribute to the activity tag to avoid showing the "normal" style of the activity :

android:theme="@android:style/Theme.Dialog"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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