应用程序只是为了开展活动 [英] App just for launching an activity
问题描述
我想添加一个快捷方式启动录音机在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"
这篇关于应用程序只是为了开展活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!