本文介绍了我onNewIntent不调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建它集成的Twitter的应用程序。我使用本教程:

 包com.blundell.tut.ttt;进口android.app.Activity;
进口android.content.Intent;
进口android.content.Shared preferences;
进口android.content.Shared preferences.Editor;
进口android.net.Uri;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.webkit.WebView;
进口android.widget.Button;
进口android.widget.Toast;
进口twitter4j.Twitter;
进口twitter4j.TwitterException;
进口twitter4j.TwitterFactory;
进口twitter4j.auth.AccessToken;
进口twitter4j.auth.RequestToken;公共类TweetToTwitterActivity延伸活动{    私有静态最后弦乐TAG =Blundell.TweetToTwitterActivity;    / **名称存储用户访问令牌* /
    私有静态最后弦乐preF_ACCESS_TOKEN =的accessToken;
    / **名称来存储用户访问令牌密钥* /
    私有静态最后弦乐preF_ACCESS_TOKEN_SECRET =accessTokenSecret;
    / **主要消费者,当您注册您的应用程序在https://dev.twitter.com/apps/产生* /
    私有静态最后弦乐CONSUMER_KEY =yourConsumerKey;
    / **消费者产生的秘密,当你注册你的应用程序在https://dev.twitter.com/apps/ * /
    私有静态最后弦乐CONSUMER_SECRET =yourConsumerSecret; // XXX恩code在您的应用程序
    / **认为Twitter会后重定向到URL中的用户登录在 - 这将由您的应用清单被拾起,并重定向到这个活动* /
    私有静态最后弦乐CALLBACK_URL =鸣叫到Twitter的布伦德尔-01机器人:///;
    / ** preferences存储的用户凭据登录* /
    私人共享preferences米preFS;
    / ** Twitter4j对象* /
    私人微博mTwitter;
    / **请求令牌表示要发送到Twitter请求的唯一ID * /
    私人RequestToken mReqToken;    私人按钮mLoginButton;
    私人按钮mTweetButton;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.i(TAG加载TweetToTwitterActivity);
        的setContentView(R.layout.activity_main);        //创建一个新的共享preference对象要记住,如果用户具有
        //已经授权我们
        米preFS = getShared preferences(推特preFS,MODE_PRIVATE);
        Log.i(TAG,得到了preferences);        //加载twitter4j帮手
        mTwitter =新TwitterFactory()的getInstance()。
        Log.i(TAG,得到Twitter4j);        //告诉twitter4j,我们希望与我们的应用程序使用
        mTwitter.setOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
        Log.i(TAG,「发水Twitter4j);        mLoginButton =(按钮)findViewById(R.id.login_button);
        mTweetButton =(按钮)findViewById(R.id.tweet_button);
    }    / **
     *按钮clickables在XML作为这个项目的分SDK宣称为1.6< / BR> < / BR>
     *检查,如果用户已经给使用Twitter这个应用程序的权限
     *前与LT; / BR>如果是这样登录并启用啁啾< / BR>
     *否则重定向到Twitter的许可
     *
     * @参数诉点击的按钮
     * /
    公共无效buttonLogin(视图v){
        Log.i(TAG,登录pressed);
        如果(M prefs.contains(preF_ACCESS_TOKEN)){
            Log.i(TAG,重复用户);
            loginAuthorisedUser();
        }其他{
            Log.i(TAG,新用户);
            loginNewUser();
        }
    }    / **
     *按钮clickables在XML作为这个项目的分SDK宣称为1.6< / BR> < / BR>
     *
     * @参数诉点击的按钮
     * /
    公共无效buttonTweet(视图v){
        Log.i(TAG,鸣叫pressed);
        tweetMessage();
    }    / **
     *创建发送到微博问'可我们的应用程序都使用Twitter此用户权限'℃的请求; / BR>
     *我们还给了{@link mReqToken}
     *这是一个独特的indetifier这一要求< / BR>
     *浏览器则Twitter网站上弹出,并在用户登录(我们从来没有看到这个,情报
     *)< / BR>微博然后重定向我们{@link CALLBACK_URL}如果登录成功< / BR>
     *
     * /
    私人无效loginNewUser(){
        尝试{
            Log.i(TAG,请求应用身份验证);
            mReqToken = mTwitter.getOAuthRequestToken(CALLBACK_URL);            Log.i(TAG启动web视图登录到Twitter的);
            的WebView twitterSite =新的WebView(本);
            twitterSite.loadUrl(mReqToken.getAuthenticationURL());
            的setContentView(twitterSite);        }赶上(TwitterException E){
            Toast.makeText(这一点,Twitter的登录错误,请稍后再试,Toast.LENGTH_SHORT).show();
        }
    }    / **
     *用户有previously给我们使用Twitter&LT应用程序的权限; / BR>
     *因此,我们检索这些凭证,并填写Twitter4j帮手
     * /
    私人无效loginAuthorisedUser(){
        字符串标记= M prefs.getString(preF_ACCESS_TOKEN,NULL);
        串秘密= M prefs.getString(preF_ACCESS_TOKEN_SECRET,NULL);        //创建一个从我们得到了previously凭据twitter的访问令牌
        在=新的accessToken(令牌,密)的accessToken;        mTwitter.setOAuthAccessToken(AT);        Toast.makeText(这一点,欢迎回来,Toast.LENGTH_SHORT).show();        enableTweetButton();
    }    / **
     *当Twitter的重定向回我们的{@link CALLBACK_URL}&LT捕捉; / BR>
     *我们使用onNewIntent作为我们的表现,我们有singleInstance =真,如果我们没了
     * getOAuthAccessToken()调用将失败
     * /
    @覆盖
    保护无效onNewIntent(意向意图){
        super.onNewIntent(意向);
        Log.i(TAG,新意图到达);
        dealWithTwitterResponse(意向);
    }    @覆盖
    保护无效onResume(){
        super.onResume();
        Log.i(TAG,在onResume抵达);
    }    / **
     * Twitter已经送我们回到我们的应用程序和LT; / BR>
     *在其设置回的意图,我们有一个钥匙,我们可以用它来验证用户
     *
     * @参数意图
     * /
    私人无效dealWithTwitterResponse(意向意图){
        URI URI = intent.getData();
        如果(URI = NULL&放大器;!&安培; uri.toString()startsWith(CALLBACK_URL)){//如果用户刚刚登录
            串oauthVerifier = uri.getQueryParameter(oauth_verifier);            authoriseNewUser(oauthVerifier);
        }
    }    / **
     *创建新用户℃的访问令牌; / BR>
     *填写Twitter4j助手< / BR>
     *和保存这些凭证,所以我们可以在下次登录直用户
     *
     * @参数oauthVerifier
     * /
    私人无效authoriseNewUser(字符串oauthVerifier){
        尝试{
            在= mTwitter.getOAuthAccessToken(mReqToken,oauthVerifier)的accessToken;
            mTwitter.setOAuthAccessToken(AT);            saveAccessToken(AT);            //设置内容视图回来后,我们换了一个网页视图
            的setContentView(R.layout.activity_main);            enableTweetButton();
        }赶上(TwitterException E){
            Toast.makeText(这一点,微博身份验证错误X01,稍后再试,Toast.LENGTH_SHORT).show();
        }
    }    / **
     *允许用户鸣叫
     * /
    私人无效enableTweetButton(){
        Log.i(TAG,用户登录 - 允许鸣叫);
        mLoginButton.setEnabled(假);
        mTweetButton.setEnabled(真);
    }    / **
     *发送鸣叫在你的时间表,以吐司味精成功或失败
     * /
    私人无效tweetMessage(){
        尝试{
            mTwitter.updateStatus(测试 - 用#Twitter4j http://blog.blundell-apps.com/sending-a-tweet/与@Blundell_apps #AndroidDev教程推特);            Toast.makeText(这一点,鸣叫成功!,Toast.LENGTH_SHORT).show();
        }赶上(TwitterException E){
            Toast.makeText(这一点,鸣叫错误,稍后再试,Toast.LENGTH_SHORT).show();
        }
    }    私人无效saveAccessToken(在的accessToken){
        字符串标记= at.getToken();
        字符串的秘密= at.getTokenSecret();
        编辑编辑= M prefs.edit();
        editor.putString(preF_ACCESS_TOKEN,令牌);
        editor.putString(preF_ACCESS_TOKEN_SECRET,秘密);
        editor.commit();
    }
}

在这里写在onNewIntent()方法不工作,我只设置的setContentView的code是一个万阿英,蒋达清?

和,这是明显的

 <应用
        机器人:图标=@绘制/图标
        机器人:标签=@字符串/ APP_NAME>
        <活动
            机器人:名字=。TweetToTwitterActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:launchMode =singleInstance>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>
                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.VIEW/>
                <类机器人:名字=android.intent.category.DEFAULT/>
                <类机器人:名字=android.intent.category.BROWSABLE/>
                <数据机器人:计划=鸣叫到Twitter的布伦德尔-01机器人/>
            &所述; /意图滤光器>
        < /活性GT;    < /用途>


解决方案

改变你的活动launchmode清单中下面给出,让我知道结果,

 的android:launchMode =singleTop

Creating an application which integrate Twitter. I use this tutorial:

http://blog.blundell-apps.com/sending-a-tweet/

package com.blundell.tut.ttt;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;

public class TweetToTwitterActivity extends Activity {

    private static final String TAG = "Blundell.TweetToTwitterActivity";

    /** Name to store the users access token */
    private static final String PREF_ACCESS_TOKEN = "accessToken";
    /** Name to store the users access token secret */
    private static final String PREF_ACCESS_TOKEN_SECRET = "accessTokenSecret";
    /** Consumer Key generated when you registered your app at https://dev.twitter.com/apps/ */
    private static final String CONSUMER_KEY = "yourConsumerKey";
    /** Consumer Secret generated when you registered your app at https://dev.twitter.com/apps/  */
    private static final String CONSUMER_SECRET = "yourConsumerSecret"; // XXX Encode in your app
    /** The url that Twitter will redirect to after a user log's in - this will be picked up by your app manifest and redirected into this activity */
    private static final String CALLBACK_URL = "tweet-to-twitter-blundell-01-android:///";
    /** Preferences to store a logged in users credentials */
    private SharedPreferences mPrefs;
    /** Twitter4j object */
    private Twitter mTwitter;
    /** The request token signifies the unique ID of the request you are sending to twitter  */
    private RequestToken mReqToken;

    private Button mLoginButton;
    private Button mTweetButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "Loading TweetToTwitterActivity");
        setContentView(R.layout.activity_main);

        // Create a new shared preference object to remember if the user has
        // already given us permission
        mPrefs = getSharedPreferences("twitterPrefs", MODE_PRIVATE);
        Log.i(TAG, "Got Preferences");

        // Load the twitter4j helper
        mTwitter = new TwitterFactory().getInstance();
        Log.i(TAG, "Got Twitter4j");

        // Tell twitter4j that we want to use it with our app
        mTwitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
        Log.i(TAG, "Inflated Twitter4j");

        mLoginButton = (Button) findViewById(R.id.login_button);
        mTweetButton = (Button) findViewById(R.id.tweet_button);
    }

    /**
     * Button clickables are declared in XML as this projects min SDK is 1.6</br> </br> 
     * Checks if the user has given this app permission to use twitter
     * before</br> If so login and enable tweeting</br> 
     * Otherwise redirect to Twitter for permission
     * 
     * @param v the clicked button
     */
    public void buttonLogin(View v) {
        Log.i(TAG, "Login Pressed");
        if (mPrefs.contains(PREF_ACCESS_TOKEN)) {
            Log.i(TAG, "Repeat User");
            loginAuthorisedUser();
        } else {
            Log.i(TAG, "New User");
            loginNewUser();
        }
    }

    /**
     * Button clickables are declared in XML as this projects min SDK is 1.6</br> </br>
     * 
     * @param v the clicked button
     */
    public void buttonTweet(View v) {
        Log.i(TAG, "Tweet Pressed");
        tweetMessage();
    }

    /**
     * Create a request that is sent to Twitter asking 'can our app have permission to use Twitter for this user'</br> 
     * We are given back the {@link mReqToken}
     * that is a unique indetifier to this request</br> 
     * The browser then pops up on the twitter website and the user logins in ( we never see this informaton
     * )</br> Twitter then redirects us to {@link CALLBACK_URL} if the login was a success</br>
     * 
     */
    private void loginNewUser() {
        try {
            Log.i(TAG, "Request App Authentication");
            mReqToken = mTwitter.getOAuthRequestToken(CALLBACK_URL);

            Log.i(TAG, "Starting Webview to login to twitter");
            WebView twitterSite = new WebView(this);
            twitterSite.loadUrl(mReqToken.getAuthenticationURL());
            setContentView(twitterSite);

        } catch (TwitterException e) {
            Toast.makeText(this, "Twitter Login error, try again later", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * The user had previously given our app permission to use Twitter</br> 
     * Therefore we retrieve these credentials and fill out the Twitter4j helper
     */
    private void loginAuthorisedUser() {
        String token = mPrefs.getString(PREF_ACCESS_TOKEN, null);
        String secret = mPrefs.getString(PREF_ACCESS_TOKEN_SECRET, null);

        // Create the twitter access token from the credentials we got previously
        AccessToken at = new AccessToken(token, secret);

        mTwitter.setOAuthAccessToken(at);

        Toast.makeText(this, "Welcome back", Toast.LENGTH_SHORT).show();

        enableTweetButton();
    }

    /**
     * Catch when Twitter redirects back to our {@link CALLBACK_URL}</br> 
     * We use onNewIntent as in our manifest we have singleInstance="true" if we did not the
     * getOAuthAccessToken() call would fail
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.i(TAG, "New Intent Arrived");
        dealWithTwitterResponse(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "Arrived at onResume");
    }

    /**
     * Twitter has sent us back into our app</br> 
     * Within the intent it set back we have a 'key' we can use to authenticate the user
     * 
     * @param intent
     */
    private void dealWithTwitterResponse(Intent intent) {
        Uri uri = intent.getData();
        if (uri != null && uri.toString().startsWith(CALLBACK_URL)) { // If the user has just logged in
            String oauthVerifier = uri.getQueryParameter("oauth_verifier");

            authoriseNewUser(oauthVerifier);
        }
    }

    /**
     * Create an access token for this new user</br> 
     * Fill out the Twitter4j helper</br> 
     * And save these credentials so we can log the user straight in next time
     * 
     * @param oauthVerifier
     */
    private void authoriseNewUser(String oauthVerifier) {
        try {
            AccessToken at = mTwitter.getOAuthAccessToken(mReqToken, oauthVerifier);
            mTwitter.setOAuthAccessToken(at);

            saveAccessToken(at);

            // Set the content view back after we changed to a webview
            setContentView(R.layout.activity_main);

            enableTweetButton();
        } catch (TwitterException e) {
            Toast.makeText(this, "Twitter auth error x01, try again later", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Allow the user to Tweet
     */
    private void enableTweetButton() {
        Log.i(TAG, "User logged in - allowing to tweet");
        mLoginButton.setEnabled(false);
        mTweetButton.setEnabled(true);
    }

    /**
     * Send a tweet on your timeline, with a Toast msg for success or failure
     */
    private void tweetMessage() {
        try {
            mTwitter.updateStatus("Test - Tweeting with @Blundell_apps #AndroidDev Tutorial using #Twitter4j http://blog.blundell-apps.com/sending-a-tweet/");

            Toast.makeText(this, "Tweet Successful!", Toast.LENGTH_SHORT).show();
        } catch (TwitterException e) {
            Toast.makeText(this, "Tweet error, try again later", Toast.LENGTH_SHORT).show();
        }
    }

    private void saveAccessToken(AccessToken at) {
        String token = at.getToken();
        String secret = at.getTokenSecret();
        Editor editor = mPrefs.edit();
        editor.putString(PREF_ACCESS_TOKEN, token);
        editor.putString(PREF_ACCESS_TOKEN_SECRET, secret);
        editor.commit();
    }
}

here the code written in onNewIntent() method is not working I setting the setcontentView only is that a probelm?

and this is the manifest

<application
        android:icon="@drawable/icon"
        android:label="@string/app_name">
        <activity
            android:name=".TweetToTwitterActivity"
            android:label="@string/app_name"
            android:launchMode="singleInstance">
            <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.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="tweet-to-twitter-blundell-01-android" />
            </intent-filter>
        </activity>

    </application>
解决方案

change your activity launchmode in manifest as given below and let me know the results,

 android:launchMode="singleTop"

这篇关于我onNewIntent不调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:34