如何识别用户来自laucher或我的Android应用程序的快捷方式?

如何识别用户来自laucher或我的Android应用程序的快捷方式?,android,android-intent,shortcut,android-pendingintent,android-broadcast,Android,Android Intent,Shortcut,Android Pendingintent,Android Broadcast,我正在开发一个android应用程序,在该应用程序中,我从HomeActivity创建一个快捷方式,然后从该快捷方式返回到HomeActivity getIntent().putExtra("key", "my url"); this.startActivity(getIntent()); this.finish(); // or instead of calling the last two do Activity.recreate() 我的问题是,我想在快捷方式中发送一些url,并想在用户

我正在开发一个android应用程序,在该应用程序中,我从
HomeActivity
创建一个快捷方式,然后从该快捷方式返回到
HomeActivity

getIntent().putExtra("key", "my url");
this.startActivity(getIntent());
this.finish(); // or instead of calling the last two do Activity.recreate()
我的问题是,我想在快捷方式中发送一些url,并想在用户从快捷方式返回到
HomeAtivity
时返回该url

getIntent().putExtra("key", "my url");
this.startActivity(getIntent());
this.finish(); // or instead of calling the last two do Activity.recreate()
以下是代码:

家庭活动

private void addShortcut() {

    Intent shortcutIntent = new Intent(getApplicationContext(),HomeActivity.class);
    shortcutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.putExtra("shortcutKey", "www.myapi.com/fromshortcut");
    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, AppConstants.shortcutTitle);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),R.drawable.ic_launcher));
    addIntent.putExtra("duplicate", false);
    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

    getApplicationContext().sendBroadcast(addIntent);

    Toast.makeText(getApplicationContext(), AppConstants.shortcutDone, 40).show();
已成功创建快捷方式。 现在我点击快捷键,返回到
HomeActivity
并获取
Intent
,但出现空指针异常

String  shortcutUrl = getIntent().getStringExtra("shortcutKey");

    if(shortcutUrl.equals("")){

        setupWebView("http://stackoverflow.com");

    }
    else{

        Log.e("shortcut was created", "url from shortcut");
        setupWebView(shortcutUrl);

    }

那么我应该怎么做才能找回数据呢?提前谢谢,准备投票选出正确的答案。

如果我答对了,您希望url作为数据从快捷方式返回您的主页
活动
,如果是,则启动
活动
,使用方法
startActivityForResult()
获取结果

下面的回调url将以
Intent
的形式返回结果

protected void onActivityResult(int requestCode, int resultCode, Intent data)          {

}

首先,当你点击主屏幕启动应用程序时,android会以默认的简单
Intent
格式打开你的应用程序,就像这样
String
格式

Intent { act=android.intent.action.MAIN 
    cat=[android.intent.category.LAUNCHER] - } // the "-" represent flags
意图
没有带
键的
捆绑
快捷键
,这就是您拥有
NPE

你的标题问题和解释让我左右为难

试试这些

1:获取
活动的意图,并通过
putExtras()
将您的url放到该活动中,然后重新创建()您的
活动

getIntent().putExtra("key", "my url");
this.startActivity(getIntent());
this.finish(); // or instead of calling the last two do Activity.recreate()
这里的问题在于您的
onCreate()
检查
Intent.hasExtras()
如果为true,则您有您的url,这意味着它已被导航到该url;如果不是,则您没有url,这意味着您通过快捷方式进入,因此您可以执行逻辑(即如果您的
家庭活动上没有
单实例

2:或者如果您在
活动中设置了
单实例
,那么您可以使用
标志
s进行检查,特别是检查问题-将指导您如何使用
标志
s进行检查


希望所有这些都是清晰和有用的

不,不,我不是从startActivity开始活动,我在这里使用的是broadcast,伙计……你不应该太多地使用应用程序上下文->当你的问题的答案以“如果我理解正确”开始时,你知道你问了一个糟糕的问题……我没有使用应用程序上下文,但得到了相同的问题。我使用了Intent.hasExtra(key);而且它是有效的,谢谢@Elltz,至少你在这里给了我提示……那么你已经解决了?如果我的答案是肯定的,你可以接受并投票sir@Rohit戈斯瓦米