【问题标题】:Linkedin integration onNewIntent() is not called in android Tabsandroid选项卡中未调用Linkedin集成onNewIntent()
【发布时间】:2013-04-18 07:45:28
【问题描述】:

我正在开发应用程序,并且在 tabhost 中我有共享设置活动。我在我的应用程序中集成了 twitter、facebook 和 linkedin。现在,我在linkedin 中遇到了问题

 @Override
 protected void onNewIntent(Intent intent)
 {
     Log.v("finish", "finish Authenticate..");

     finishAuthenticate(intent.getData());
 }

没有被调用,因为我使用了 tabhost。没有 tabhost 它工作得很好..

我通过 android:launchMode="singleTop" 和 android:launchMode="singleTask" 应用 Manifest 文件中的更改以及 android:launchMode="singleInstance" bt 它根本不起作用..

【问题讨论】:

    标签: android linkedin


    【解决方案1】:

    以下代码非常适合我

    通过以下代码,我已成功完成 100% 测试

        public class ShareInLinkedIn extends Activity implements OnClickListener {
    
    private LinkedInOAuthService oAuthService;
    private LinkedInApiClientFactory factory;
    private LinkedInRequestToken liToken;
    private LinkedInApiClient client;
    public static final String LINKEDIN_PREF = "GamePrefs";
    
    @SuppressLint({ "NewApi", "NewApi", "NewApi" })
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linkedin);
    
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
    
        oAuthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET, Constants.SCOPE_PARAMS);
        System.out.println("oAuthService : " + oAuthService);
    
        factory = LinkedInApiClientFactory.newInstance(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
    
        liToken = oAuthService.getOAuthRequestToken(Constants.OAUTH_CALLBACK_URL);
        System.out.println("onCreate:linktoURL : " + liToken.getAuthorizationUrl());
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(liToken.getAuthorizationUrl()));
        startActivity(i);
    
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    
        try {
            linkedInImport(intent);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
    
    private void linkedInImport(Intent intent) {
        String verifier = intent.getData().getQueryParameter("oauth_verifier");
        System.out.println("liToken " + liToken);
        System.out.println("verifier " + verifier);
    
        LinkedInAccessToken accessToken = oAuthService.getOAuthAccessToken(liToken, verifier);
        //SharedPreferences settings = getSharedPreferences(LINKEDIN_PREF, MODE_PRIVATE);
        // final Editor edit = settings.edit();
        // edit.putString(OAuth.OAUTH_TOKEN, accessToken.getToken());
        // edit.putString(OAuth.OAUTH_TOKEN_SECRET,
        // accessToken.getTokenSecret());
        // edit.putString("linkedin_login", "valid");
        // edit.commit();
    
        client = factory.createLinkedInApiClient(accessToken);
    
        // client.postNetworkUpdate("LinkedIn Android app test");
    
        Person profile = client.getProfileForCurrentUser(EnumSet.of(ProfileField.ID, ProfileField.FIRST_NAME, ProfileField.LAST_NAME, ProfileField.HEADLINE));
    
        System.out.println("First Name :: " + profile.getFirstName());
        System.out.println("Last Name :: " + profile.getLastName());
        System.out.println("Head Line :: " + profile.getHeadline());
    
        OAuthConsumer consumer = new CommonsHttpOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
        consumer.setTokenWithSecret(accessToken.getToken(), accessToken.getTokenSecret());
    
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost post = new HttpPost("https://api.linkedin.com/v1/people/~/shares");
        try {
            consumer.sign(post);
            post.setHeader("content-type", "text/XML");
            String myEntity = "<share><comment>This is a test</comment><visibility><code>anyone</code></visibility></share>";
            post.setEntity(new StringEntity(myEntity));
            org.apache.http.HttpResponse response = httpclient.execute(post);
            // Get the response
            BufferedReader rd = new BufferedReader
              (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer strBfr = new StringBuffer();   
            String line = "";
            while ((line = rd.readLine()) != null) {
    
                strBfr.append(line);
            } 
            System.out.println("Response is : "+strBfr.toString());
            Toast.makeText(ShareInLinkedIn.this, strBfr.toString(), Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    
    }
    
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    
    }
    

    }

    常量.java

        public class Constants {
    
    public static final String CONSUMER_KEY = "YOUR_CONSUMER_KEY";
    public static final String CONSUMER_SECRET = "YOUR_CONSUMER_SECRET_KEY";
    public static final String OAUTH_CALLBACK_SCHEME = "x-oauthflow-linkedin";
    public static final String OAUTH_CALLBACK_HOST = "litestcalback";
    public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST;
    public static final String SCOPE_PARAMS = "rw_nus+r_basicprofile";
    

    }

    AndroidManifiest.xml 文件

          <activity
            android:name="com.linkedin.ShareInLinkedIn"
            android:launchMode="singleInstance" >
            <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:host="litestcalback"
                    android:scheme="x-oauthflow-linkedin" />
            </intent-filter>
        </activity>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    相关资源
    最近更新 更多