应用程序只是为了开展活动应用程序

2023-09-06 23:35:25 作者:倾暖微嫣°

我想添加一个快捷方式启动录音机在App抽屉(没有主屏幕)

因此​​,应用程序应该是空的,它要做的唯一工作就是启动录音机 com.android.soundrecorder / com.android.soundrecorder.SoundRecorder

我怎么能这样做?

我得到强制停止使用这样的:

`

 <使用-SDK
    安卓的minSdkVersion =16
    机器人:targetSdkVersion =15/>

<应用
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>
    <活动
        机器人:录像机NAME =
        机器人:标签=@字符串/ title_activity_recorder>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>

            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>
 

`

和Java文件

 `包装recorder.audio.dsaif;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;

公共类录音机延伸活动{

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    意图LaunchIntent = getPackageManager()getLaunchIntentForPackage(com.android.soundrecorder.SoundRecorder)。
    startActivity(LaunchIntent);
    的setContentView(R.layout.activity_recorder);
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    。getMenuInflater()膨胀(R.menu.activity_recorder,菜单);
    返回true;

}
}`
 
为什么我下的反恐行动不能玩打开出现应用程序错误

解决方案

我认为你需要添加一个新任务的标志,也称光洁度()和删除的setContentView。

如果崩溃是由一个空指针异常,也许包是不正确和/或不安装的应用程序。

这就是为什么你需要检查的目的是给你或为空。

顺便说一句,变量名应该有一个小写字母。

code在这里的活动:

  @覆盖
  保护无效的onCreate(最终捆绑savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    最终意图launchIntent=getPackageManager().getLaunchIntentForPackage("com.android.soundrecorder.SoundRecorder");
    如果(launchIntent == NULL)
      {
      Toast.makeText(这一点,无法找到该应用程序推出!,Toast.LENGTH_SHORT).show();
      完();
      返回;
      }
    launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(launchIntent);
    完();
    }
 

在清单文件,这个属性添加到活动标签避免显示该活动的正常的风格:

 安卓主题=@安卓风格/ Theme.Dialog
 

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;

}
}`

解决方案

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"