我的 onNewIntent 没有调用

2023-11-26

创建一个集成 Twitter 的应用程序。我使用这个教程:

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();
    }
}

这里 onNewIntent() 方法中编写的代码不起作用我仅设置 setcontentView 这是一个问题吗?

这是清单

<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>

如下所示更改清单中的活动启动模式,并让我知道结果,

 android:launchMode="singleTop"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我的 onNewIntent 没有调用 的相关文章

  • 代码如何从 Android Gallery 加载图像

    我有用于从图库加载图像的代码 但我真的不明白它是如何工作的 这是代码 Override protected void onActivityResult int requestCode int resultCode Intent data s
  • 如何在flutter android插件包中处理android生命周期

    我需要知道 android 插件包中 flutter 应用程序视图的当前状态 现在 我观察颤振视图中的状态https docs flutter io flutter widgets WidgetsBindingObserver class
  • 如何检测手机一整圈(360 度)?

    由于我在网络上搜索没有找到任何相关答案 因此我发布了有关检测的问题360度转弯使用加速度计绕其轴的 Android 设备 例如 在横向模式下围绕 y 轴 假设在起始位置 y 值为 0 设备平坦于地面 当手机向前旋转 90 度时y 10 18
  • 清除堆内存以防止内存不足异常

    我知道已经有人问过有关内存不足的问题 但我没有找到解决方案 在位图工厂中 我出现内存不足异常 甚至使用 inSampleSize 1 所以我经常用 try catch 内存不足异常来包围它 因为这是一个不好的做法 try catch Out
  • React-Native 中的导航抽屉

    我是反应原生的新手 不介意我问一个基本问题 我想知道 实现抽屉式导航的分步过程是什么 推荐链接这个链接 https github com react native community react native side menu usage
  • SDK 管理器缺少模拟器的旧版 Android 系统映像

    我刚刚重新安装了 ADT 捆绑包 20130522 和 Android Studio 因为我的 eclipse 安装再次搞砸了 但那是另一个故事了 在任一版本中 当我启动 SDK Manager 时 都没有任何 2 2 的系统映像 只有 2
  • Android 操作栏 SearchView 作为自动完成功能?

    我在操作栏中使用 SearchView 我想在搜索视图上使用自动完成功能来从数据库中获取结果 这可能吗 或者我是否需要使用自定义文本框 然后添加自动完成功能 所以我只需要对 v7 版本执行此操作 并沮丧地发现我不能简单地使用 ArrayAd
  • 从txt文件中读取数据而不下载它?

    我想从提供的文本文件中解析信息 有没有一种方法可以在应用程序中执行此操作 而无需先下载文件 以某种方式传输文本内容 打开到 URL 的 Http 连接 使用内置 HttpURLConnection 或使用 commons httpclien
  • 在哪里以及如何安装 twitter 的 Python API?

    我访问了 Twitter 的 API 它把我重定向到了谷歌代码 但网站不在那里 有其他替代的 Twitter API 以及教程吗 谢谢 尝试推推 http code google com p tweepy http code google
  • Google play APK 上传错误重复权限声明

    我尝试上传新的 apk 使用我自己的发布密钥存储签名 并在 google play 开发者控制台中收到以下错误 重复的许可声明 android permission ACCESS COARSE LOCATION 与不同 maxSdkVers
  • Android Studio 与 Google Play 服务的编译问题

    我正在运行 Android Studio 0 8 4 并在 Android Studio 0 8 2 上尝试过此操作 我正在运行 Java JDK 1 8 0 11 并尝试使用 JDK 1 8 0 05 每当我尝试构建我的 android
  • 彩信数据总是错误

    我正在从 Galaxy S6 读取短信和彩信数据 所有 SMS 消息都有一个date像这样的字段1456252633000 该数字是 unix 时间戳 1000 我发送 未收到 的彩信具有如下日期字段 1440628863时间戳是正确的 u
  • 定时器时间不作为变量改变?

    这是我的代码 private int V Time 1 try final Timer V Timer final Handler V Handler V Timer new Timer V Handler new Handler Loop
  • Android TextView 中的等宽表格数字

    我有一个自定义字体 默认情况下具有可变宽度数字字形 并且我想在 Android 中使用该字体的等宽表格数字功能TextView使数字垂直对齐 也就是说 改变如下 像这样的事情 要选择字体的表格数字功能 请使用TextView s fontF
  • 在 Unity 中构建 apk 应用程序时包含文件

    在unity中构建apk文件时如何将文件和文件夹添加到apk文件中 我需要的是在Android上安装应用程序后 在应用程序的父目录 android data com company product files 中存在一些文件和文件夹 这是我
  • android gradle插件-离线安装

    我必须在离线电脑上安装 android gradle 插件 通过谷歌搜索 我了解到我可以通过本地 Maven 存储库来做到这一点 但从不成功的尝试和所有关于这个问题的质量保证中我知道这并不简单 我从来没有和maven一起工作过 有经验的人可
  • 在异步请求中使用超时回调

    我之前问过这个问题 但我将用提出的解决方案来完成这个问题 并提出另一个问题 我正在使用这个类来进行异步网络请求 http msdn microsoft com en us library system net webrequest aspx
  • 如何在捆绑中存储稀疏数组

    我有一个SparseArray
  • Desire HD 中的应用程序崩溃

    由于某些莫名其妙的原因 我的应用程序在 HTC Desire HD 上崩溃了 它在其他手机和模拟器中运行良好 这是崩溃报告 java lang RuntimeException Unable to start activity Compon
  • 如何检查设备上是否安装了电子邮件客户端

    我需要检查设备上是否安装了电子邮件客户端 我使用了以下代码 但它对我不起作用 public boolean isIntentAvailable final PackageManager packageManager getApplicati

随机推荐

  • C++ 模板模板参数的语法

    我很难理解 C 模板模板参数的语法 根据精彩的描述 我明白它们为什么有用here 我只是发现他们的语法很难理解 两个例子取自上述网站 还有其他 template
  • 简单注入器,无法覆盖现有注册

    我目前是第一次使用简单注射器 在我的 NET 项目中 我正在运行测试和模拟从 Web 服务返回的数据 并将对象注册到容器 如下所示 container Register
  • 如何在 virtualenv 中添加 PYTHONPATH 路径

    我正在尝试添加 PYTHONPATH 环境变量的路径 该路径仅在特定的 virtualenv 环境中可见 I tried SET PYTHONPATH 在 virtualenv 命令提示符下 但这会为整个环境设置变量 我该如何实现这一目标
  • 空 div 与具有 inline-block 属性的文本的 div

    想知道这种行为的原因 CSS div display inline block margin right 2px width 20px background color red 空div div style height 20px div
  • 远程事后核心转储分析,无需共享系统库的精确调试符号

    您通常如何解决这个问题 想象一下 一个线程在 Computer1 上的 libc 代码 系统共享库 内部崩溃 然后生成核心转储 但要分析此 coredump 的 Computer2 可能具有不同版本的 libc So 在远程计算机上拥有相同
  • Material Design:如何断开浮动标签以供选择?

    我需要创建没有浮动标签的选择字段 但我想要有占位符和默认值 我阅读文档https material angular io components form field overview floating label并尝试自己做
  • 如何将数据库上传到 Heroku

    我有一个共享的heroku应用程序 现在我不想使用相同的代码在heroku中创建一个测试应用程序 所以我创建了一个新的应用程序 好的 问题是要使该应用程序正常工作 它需要一个数据库 所以我正在尝试上传本地数据库 但不知道如何上传 谁能告诉我
  • 如何在 条目中获取红色星号

    如何在条目中添加红色星号 以便可以将其显示在文本末尾以指示其为必填字段 例如 输入您的姓名 星号将为红色 或者 就此而言 文本中的任何位置 您无法通过 xml 字符串资源来做到这一点 这只能通过代码来完成 为此 您需要使用Spannable
  • 将 mysql 跳过网络设置为关闭

    我正在尝试设置我的 Ubuntu 12 10 服务器以接受远程 mysql 连接 但是我在将跳过网络设置为关闭时遇到困难 注意我已经将绑定地址设置为面向互联网的 IP 而不是 127 0 0 1 当我查看 etc mysql my conf
  • jQuery.parseJSON 与 JSON.parse

    jQuery parseJSON and JSON parse是执行相同任务的两个函数 如果 jQuery 库已经加载 将使用jQuery parseJSON比使用更好JSON parse 在性能方面 如果是 为什么 如果不是 为什么不呢
  • 如何使用 Python 高效解析电子邮件而不触及附件

    我正在使用 Python imaplib Python 2 6 从 GMail 获取电子邮件 我用方法获取电子邮件的所有内容http docs python org library imaplib html imaplib IMAP4 fe
  • CSS 悬停与 JavaScript 鼠标悬停 [关闭]

    Closed 这个问题是基于意见的 目前不接受答案 有时我需要选择使用 CSS element hover 还是 JavaScript onmouseover 来控制页面上 html 元素的外观 考虑以下场景 其中 div 包装输入 div
  • SVG 图像在 IE9 中不会被裁剪

    以下代码 div style border solid 1px black height 100px width 100px div
  • Scala:合并地图

    如何合并地图 如下所示 Map1 Map 1 gt Class1 1 2 gt Class1 2 Map2 Map 2 gt Class2 1 3 gt Class2 2 合并后 Merged Map 1 gt List Class1 1
  • 空列表到可见性转换器

    我正在尝试为 WPF 做一个 空列表到可见性转换器 这是一个 IValueConverter 它接受一个对象 应该是一个列表 如果列表为空 或者传递的对象为 null 它应该返回 Visibility Collapsed 如果列表不为空 则
  • 迄今为止的 Swift ISO8601 格式

    我有以下字符串 20180207T124600Z 如何将其转换为 Date 对象 这是我当前的代码 但它返回nil let dateString 20180207T124600Z let dateFormatter ISO8601DateF
  • Spring Web 响应式客户端

    我正在尝试使用 Spring Reactive WebClient 将文件上传到 spring 控制器 控制器非常简单 如下所示 PostMapping value upload consumes MediaType MULTIPART F
  • Azure DevOps REST api - 使用变量运行管道

    我在 Azure Devops 上有一个管道 我尝试使用 REST api 以编程方式 无头运行 https learn microsoft com en us rest api azure devops pipelines runs ru
  • 从嵌入 iframe 代码获取 YouTube 视频 ID

    我想使用 preg match 或正则表达式从 YouTube 嵌入代码获取 YouTube 视频 ID 举个例子 我要拿身份证0gugBiEkLwU 谁能告诉我该怎么做 真的很适合你的帮助 将此模式与捕获组一起使用应该会为您提供所需的字符
  • 我的 onNewIntent 没有调用

    创建一个集成 Twitter 的应用程序 我使用这个教程 http blog blundell apps com sending a tweet package com blundell tut ttt import android app