Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在主屏幕上设置快捷方式的启动意图?安卓_Android_Android Intent_Intentfilter - Fatal编程技术网

Android 如何在主屏幕上设置快捷方式的启动意图?安卓

Android 如何在主屏幕上设置快捷方式的启动意图?安卓,android,android-intent,intentfilter,Android,Android Intent,Intentfilter,我对android非常陌生,我很难把它记下来。我已经成功地从我的应用程序中创建了一个快捷方式。唯一的问题是,我无法规定在单击快捷方式后会发生什么。它只是启动我的MainActivity,但我希望它在选择MainActivity时也传递数据。这是我所拥有的 Intent shortcutIntent = new Intent(getActivity().getApplicationContext(), MainActivity.class); shortcutIn

我对android非常陌生,我很难把它记下来。我已经成功地从我的应用程序中创建了一个快捷方式。唯一的问题是,我无法规定在单击快捷方式后会发生什么。它只是启动我的MainActivity,但我希望它在选择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>

您正在做的是发送广播。。。。这就是为什么你得到空值

getActivity().getApplicationContext().sendBroadcast(addIntent);
这就是为什么在包中得到null

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
    }
};
您还需要设置一个intentFilter,您可以在onCreate方法中注册dynamic,如

IntentFilter filter = new IntentFilter(=====SOME CONSTANT THAT YOU DEFINE AND THAS PASSED FROM THE STARTING INTENT====);
registerReceiver(noteCompletedReceiver, filter);
在活动的onDestroy方法中,您需要调用

unregisterReceiver(noteCompletedReceiver);

我应该如何实现广播接收器?动态注册和注销我解释了如何实现