Sending Intent inside of another Intent

2019-05-03 07:45发布

Perhaps I am going about this the wrong way, but I want to respond to my Android AppWidget's click event within my own app AND launch an Activity. At the time I set the PendingIntent I have another Intent which I want to launch when clicked. My onStartCommand uses this line:

final Intent mLaunchIntent = (Intent) intent.getParcelableExtra(Widget.EXTRA_INTENT);

When I call setOnClickPendingIntent I have this line prior:

mSendingIntent.putExtra(Widget.EXTRA_INTENT, (Parcelable) mLaunchIntent);

So even though mLaunchIntent is a valid Intent in both lines, the first line is missing a great deal of data. Calling startActivity then fails because the Intent is not valid.

I am wondering if it is possible, and how, to send an Intent inside of another Intent without strictly calling putExtras because that method simple adds the extras from one Intent to the next. I'd like to keep these two separate and easily accessible.

4条回答
一夜七次
2楼-- · 2019-05-03 07:57

You can put an Intent to an Intent with:

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_INTENT, new Intent());

To retrieve the Intent (from within an Activity) from the intent you can do the following:

Intent intent = getIntent().getParcelableExtra(Intent.EXTRA_INTENT);
查看更多
太酷不给撩
3楼-- · 2019-05-03 08:20

There are different ways you can pass intents / objects from source to destination and vice versa. One way of doing it without using bundles or extras would be resorting to the usual class methods with variables (getters and setters). Pass the objects using methods. Another way of doing it would be the use of class variables. Ex:

public class ClassB  extends Activity{
    public static Object myObject;
    ...
}

public class ClassA extends Activity{
    ...
    @override
    protected void onCreate(Bundle savedInstanceState){
        Object myObject = ClassB.myObject;
    }
}
查看更多
聊天终结者
4楼-- · 2019-05-03 08:21

Can't you use a service to parse the Intent?

查看更多
手持菜刀,她持情操
5楼-- · 2019-05-03 08:23

I actually figured it out, the solution was quite simple. mLaunchIntent should not be cast to Parcelable or the data gets lost.

mSendingIntent.putExtra(Intent.EXTRA_INTENT, mLaunchIntent);

That was all that I needed to send an Intent through another Intent.

查看更多
登录 后发表回答