在我Application
水平我收到空为getExtras()
,但Activity
水平,我可以正确地看到它们。getExtras()返回在应用层(-class)空
public class MyApplication extends Application
{
@Override
public void onCreate() {
super.onCreate();
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.MyApp");
if (intent != null){
String mStaticWorldUrl = intent.getStringExtra("arg1Name");
String mStaticWorldIconUrl = intent.getStringExtra("arg2Name");
Log.i("LOG", mStaticWorldUrl + " --- " + mStaticWorldIconUrl);
}
}
}
我打电话从这个代码创建的快捷方式的一些应用:
( - 每个快捷方式都有不同的Extras
发送到Intent
)
// create a shortcut for the specific app
public static void createShortcutForPackage(Context context,
String packageName, String className, String shortcutName,
String arg1Name, String arg1Val, String arg2Name, String arg2Val,
int iconID) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, className));
PackageManager pm = context.getPackageManager();
Context pkgContext = createPackageContext(context, packageName);
if (pkgContext == null)
return;
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Intent shortcutIntent = pm.getLaunchIntentForPackage(packageName);
if (arg1Name != null)
shortcutIntent.putExtra(arg1Name, arg1Val);
if (arg2Name != null)
shortcutIntent.putExtra(arg2Name, arg2Val);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, iconID));
shortcut.putExtra("duplicate", false);
context.sendBroadcast(shortcut);
}
我如何可以读取这些Extras
关于Application
的等级?
或者是否有任何其他方式来创建应用程序的不同快捷方式,并在Application
上读取其“'参数”数据?
是@RedHat没有得到额外的原因是因为查询的意图是一个产生早一行,这是该操作系统已为包产生如下面的代码要求的意图:意向意图= getPackageManager()getLaunchIntentForPackage( “com.example.MyApp”); – yakobom
是的,这是正确的:'PackageManager'方法返回一个'Intent',它可以用来启动指定的包*,它与用于启动包的组件的实际'Intent'不同)@但是,我的回答仍然有效:你不想看'Application'类实例中的'Intent'对象:它们不在那里传递。 –
是@LarrySchiefer,你当然是对的。然而,指出另一个错误是重要的,因为这是一个概念错误。将相同的代码移至活动级别也会导致相同的问题。 – yakobom