我是安卓系统的新手,很难理解这一点。我已经成功地从我的应用程序中创建了一个快捷方式。唯一的问题是,我无法决定点击快捷方式后会发生什么。它只是启动我的MainActivity,但我希望它在选择主活动时也向它传递数据。这是我的。。。
Intent shortcutIntent = new Intent(getActivity().getApplicationContext(),
MainActivity.class);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, f.getName());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(getActivity().getApplicationContext(),
R.drawable.icon_folderbluegray));
addIntent.putExtra("info for Main Activity","Hello");
addIntent
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getActivity().getApplicationContext().sendBroadcast(addIntent);
但当我这样做的时候,我会变得无效。。。
Bundle extras = getIntent().getExtras();
这是我在美茵节上得到的东西。
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
您正在做的是sendigBroadcast。。。。这就是为什么你得到空
getActivity().getApplicationContext().sendBroadcast(addIntent);
这就是为什么你在这里的捆绑包中得到空
Bundle extras = getIntent().getExtras();
为了处理结果,您需要实现广播接收器
编辑实现接收器
public final BroadcastReceiver noteCompletedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(NoteListActivity.this, "Broadcast received", Toast.LENGTH_LONG).show();
//HANDLE HERE THE INTENT
}
};
此外,您还需要设置intetFilter,您可以在onCreate方法中注册动态,如
IntentFilter filter = new IntentFilter(=====SOME CONSTANT THAT YOU DEFINE AND THAS PASSED FROM THE STARTING INTENT====);
registerReceiver(noteCompletedReceiver, filter);
在你活动的onDestroy方法中,你需要调用
unregisterReceiver(noteCompletedReceiver);