簡體   English   中英

如何在Android中應用StrictMode

[英]How to apply StrictMode in android

我是不熟悉android的人,我正在一個與android應用程序共享twitter推文的android應用程序中工作,但收到一條錯誤requestRequestToken引發“與服務提供商的通信失敗:null”。 我從堆棧溢出獲得了一個解決方案(http://stackoverflow.com/questions/8534831/method-retrieverequesttoken-raises-communication-with-the-service-provider-fail)。 它說我們將能夠通過StrictMode解決此問題。 我不知道該如何應用。

這是ma代碼:

try {

            Log.i(TAG, "Retrieving request token from Google servers");
            final String url = provider.retrieveRequestToken(consumer,Constants.OAUTH_CALLBACK_URL);                                                
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
                    .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                            | Intent.FLAG_ACTIVITY_NO_HISTORY
                            | Intent.FLAG_FROM_BACKGROUND);         
            context.startActivity(intent);



        } catch (Exception e) {

            Log.e("Error",e+"");
        }

請幫我。 提前致謝。

試試吧 -

private void enableStrictMode(Bundle savedInstanceState) {
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                                             .detectDiskReads()
                                             .detectDiskWrites()
                                             .detectNetwork()
                                             .penaltyLog()
                                             .build());

    super.onCreate(savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    enableStrictMode(savedInstanceState);
    setContentView(layout.main);
}

Suvan Roy提供的答案,它將幫助您在控制台中看到一種方法花費太多時間(在UI Thread中),而不是為什么它不起作用的原因。

我的建議:

  • 激活StricMode,但僅在調試模式下激活,而不在Google Play版本中激活,並始終在控制台日志中檢查“嚴格模式”錯誤

在您的應用程序類(如果有的話)和活動中執行此操作

    @Override 
    public void onCreate(Bundle savedInstanceState) {
       if (BuildConf.DEBUG){
             StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                                             .detectDiskReads()
                                             .detectDiskWrites()
                                             .detectNetwork()
                                             .penaltyLog()
                                             .build());
    }
    super.onCreate(savedInstanceState);
       // your code here
    }

為了更好地利用資源,我建議還通過Handler或AsynchTask調用網絡提供商。 查看包含您的代碼的示例:

new Handler().post( new Runnable()
{
           @Override
           public void run()
           {
              Log.i(TAG, "Retrieving request token from Google servers");
              final String url = provider.retrieveRequestToken(consumer,Constants.OAUTH_CALLBACK_URL);                                                
              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
                      .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                              | Intent.FLAG_ACTIVITY_NO_HISTORY
                              | Intent.FLAG_FROM_BACKGROUND);         
              context.startActivity(intent);

           }
} );

希望您找到問題的根源,並利用我的建議;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM