开发者

Android StartActivityForResult for reusable code - any other ways of doing this? (regarding Twitter)

开发者 https://www.devze.com 2023-01-15 01:10 出处:网络
Currently I\'m trying to implement a reusable Twitter status \'poster\'.The Twitter status part of it is working, but the reusable bit isn\'t.Here\'s what I\'m doing:

Currently I'm trying to implement a reusable Twitter status 'poster'. The Twitter status part of it is working, but the reusable bit isn't. Here's what I'm doing:

An activity called SummaryDisplay imports a class TweetStatus and calls a function sendStatus. It also implements a listener which is called when TweetStatus can confirm that the status was successfully sent. So far so good.

TweetStatus sends an intent to another activity, OAUTH, which will return with the user token and secret allowing us to 'login' to Twitter.

If OAUTH can't find any saved tokens and secrets, it sends an intent to the browser so the user can login to Twitter and confirm that our app is allowed to send tweets on their behalf. Twitter than uses a callback to return to the OAUTH application (by using an intent filter within the manifest we make our own android:scheme and host):

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="my-app" android:host="twitter-login-success" />
 </intent-filter>

The callback URI is then:

private static final Uri CALLBACK_URI = Uri.parse("my-app://twitter-login-success");

It is because of this callback being an intent that the OAUTH class has to extend activity (so it can receive these intents).

So the problem is in OAUTH returning with the token and secret (shown in bold above). Currently OAUTH sends an intent to SummaryDisplay that then sends the data to TweetStatus. The problem is that the SummaryDisplay intent is hardcoded:

Intent i = new Intent(this, SummaryDisplay.class);

so the OAUTH class is no longer reusable as a new one will need to be made for each activity 开发者_开发技巧which wants to send a Twitter status.

One option is for the callback from Twitter to be changed to SummaryDisplay instead of OAUTH, but then this requires changing the callback to one of your choice (one not registered with Twitter and I need to check if that's allowed). Another option which I'm trying is to use StartActivityForResult.

So:

TweetStatus calls OAUTH with StartActivityForResult,

OAUTH calls the browser,

Browser then calls OAUTH back using a callback URI,

OAUTH returns result to whomever started it.

Therefore reusable. However the callback URI received from the browser is seemingly creating a new instance of OAUTH which has no idea who to callback, so just does nothing. I've tried setting the android:launchMode to singleInstance but this disables the result from being sent (with LogCat reporting):

09-08 10:22:00.742: WARN/ActivityManager(563): Activity is launching as a new task, so cancelling activity result.

Are there any other ways of doing this? Is there, for example, a way of sending a "class" of yourself in an intent which can then be used in

Intent i = new Intent(this, myClassVariableSetUsingAnIntent);

So I can then use

android:launchMode="singleInstance"

Thanks for any help.


Is there, for example, a way of sending a "class" of yourself in an intent...

You could use createPendingResult() to pass an object in an Intent extra that can be used later to trigger onActivityResult(). Or, you can do a private "broadcast", specifying an action and package to restrict the scope to only go back to the originating app. Here is a sample client and remote service that demonstrate the use of these two -- in your case, you wouldn't be using them from a service, of course, but it's the only sample code I have handy. One of these should, in principle, let you get your data back to the original activity. From there, it is a matter of finish()-ing everything between where you are and that activity.


Possible alternative is to reuse the activity by setting flags for the intents. Details here: http://caguilartech.blogspot.com/2010/07/android-intent-switching-reusing.html

0

精彩评论

暂无评论...
验证码 换一张
取 消