简体   繁体   English

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

[英]How can I set up the launch intent for shortcut on home screen? Android

I am very new to android and I am having trouble getting this down. 我对android非常陌生,无法解决这个问题。 I have been succesfully able to create a shortcut from within my app. 我已经能够在我的应用程序内成功创建快捷方式。 The only problem is that I cannot dictate what happenes after the shortcut is clicked. 唯一的问题是,我无法确定单击快捷方式后会发生什么。 It just launches my MainActivity, but I want it to also pass it data when the main activity is selected. 它只是启动我的MainActivity,但是我希望它在选择主要活动时也传递数据。 Here is what I have... 这就是我所拥有的...

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);

But when I do this, I get null... 但是当我这样做时,我得到的是空值...

Bundle extras = getIntent().getExtras();

Here is what I have in the Mainfest. 这是我在Mainfest中所拥有的。

             <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>

what you are doing is sendigBroadcast.... thats why you are getting null 您正在做的是sendigBroadcast ....这就是为什么您得到null的原因

getActivity().getApplicationContext().sendBroadcast(addIntent);

that's why you get null in the bundle here 这就是为什么您在此处的捆绑商品中获得null的原因

Bundle extras = getIntent().getExtras();

To handle the result you need to implement a broadcast receiver 要处理结果,您需要实现广播接收器

EDIT IMPLEMENT THE RECEIVER 编辑收货人

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
    }
};

also you need to set a intentFilter, you can register dynamic in your onCreate method like 您还需要设置一个intentFilter,可以在onCreate方法中注册动态

IntentFilter filter = new IntentFilter(=====SOME CONSTANT THAT YOU DEFINE AND THAS PASSED FROM THE STARTING INTENT====);
registerReceiver(noteCompletedReceiver, filter);

and in onDestroy method of your activity you need to call 在您活动的onDestroy方法中,您需要调用

unregisterReceiver(noteCompletedReceiver);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM