【问题标题】:How to get the proper actions from intent on android如何从 Android 上的意图中获取正确的操作
【发布时间】:2017-08-25 05:11:15
【问题描述】:

有两种应用:Caller,Callee

案例一>

当我从主屏幕启动 Callee 应用程序时,我可以从 INTENT 获取一个操作(“android.intent.action.MAIN”)。

当 Callee 运行时,我再次从 Caller 应用程序启动 Callee 应用程序并执行一个操作(“andoird.intent.action.test”)。然后我得到了动作(“android.intent.action.MAIN”)。不是动作(“andoird.intent.action.test”)。

我怎样才能得到动作(“andoird.intent.action.test”)?

案例 2 >

当我使用 action("andoird.intent.action.test") 启动 Callee 应用程序时,我得到了 action("andoird.intent.action.test")。

在 Callee 运行时,我从主屏幕启动 Callee 应用程序。然后我得到了动作(“andoird.intent.action.test”)。不是动作(“android.intent.action.Main”)。

如何获取操作(“android.intent.action.Main”)?

为了得到正确的操作,我缺少什么?

下面是调用者代码。

public class CallerActivity extends AppCompatActivity {
public static final String ACTION_TEST = "android.intent.action.test";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button test = (Button) findViewById(R.id.gotoSetting);
    test.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = getPackageManager().getLaunchIntentForPackage(PACKAGE);
            intent.setAction(ACTION_TEST);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
        }
    });
}

}

下面是被调用者代码。

public class CalleeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.e("test", "onCreate()");
    Log.e("test", getIntent().getAction());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.e("test", "onNewIntent()");
    Log.e("test", getIntent().getAction());
}

}

这是 Callee 的 AndroidManifest。

        <activity
        android:name=".CalleeActivity"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden">
        <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.test"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

请帮忙。

【问题讨论】:

    标签: android android-intent action


    【解决方案1】:

    替换此代码:

        @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e("test", "onNewIntent()");
        Log.e("test", getIntent().getAction());// getIntent() will return old intent
    }
    

        @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.e("test", "onNewIntent()");
        Log.e("test", intent.getAction()); //use new intent
    }
    

    【讨论】:

    • 天啊!!那是我的大错!!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多