1

facebook.com/l.php でラップされた URL を「クリーン」にするアプリを作成しようとしています。コードは正常にコンパイルされ、Facebook アプリから呼び出すこともできますが、黒い画面が表示され、解析した新しい URL が処理されないようです。

Javaは初めてではありませんが、ドロイドは初めてです..... :-o

とにかく、次のように明示します。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fusco.leetum.fblinkcleaner"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="fusco.leetum.fblinkcleaner.Cleaner"
        android:label="@string/app_name" >
        <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" />
            <data android:scheme="http" android:host="facebook.com" /> 
            <data android:scheme="http" android:host="m.facebook.com" />
            <data android:scheme="http" android:host="www.facebook.com" />
        </intent-filter>
    </activity>
</application>

</manifest>

アクティビティ Java (ユーザーが開始しても何もしないはずなので、finish() がありますが、パーサー コードは onCreate() メソッドにあるため、毎回開始する必要があります):

    package fusco.leetum.fblinkcleaner;

import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; public class Cleaner extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_cleaner); final Intent intent = getIntent(); if (intent.getAction().equals(Intent.ACTION_VIEW)) { final String baseurl = intent.getDataString(); String newurl = baseurl; if (baseurl.indexOf("?") != -1) { String[] parseurl = baseurl.substring(baseurl.indexOf("?")+3).split("%2F"); newurl = "http:/"; for (int i=2; i < parseurl.length; i++) { newurl = "/"+parseurl[i]; } } final Intent viewnewpage = new Intent(Intent.ACTION_VIEW, Uri.parse(newurl)); startActivity(viewnewpage); } this.finish(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_cleaner, menu); return true; } }
4

1 に答える 1

0

愚かな私!独自のパーサーを作成する代わりに、Uri クラスのものを使用する必要がありました。なんという学習曲線。Android は、通常のデスクトップ プログラミングとは少し異なります。まあ、マニフェストは同じですが、「より良い」コードは次のとおりです。おそらく最高ではありませんが、それでも、これが他の誰かに役立つことを願っています:-D

これは onCreate(...) メソッドで行われ、super 呼び出し以外はすべて置き換えられます。

Intent intent = this.getIntent(); // get intent that started activity

    if (intent.getAction().equals(Intent.ACTION_VIEW)) { // if called by view action
        Uri raw = intent.getData(); // get raw uri
        Uri cleanedUri = raw; // assign to allow 'pass thru' of not a wrapper
        String wrappedUri = raw.getQueryParameter("u"); // get the url defined in l.php?u=

        if (wrappedUri != null) { // if there was a u= parameter
            cleanedUri = Uri.parse(Uri.decode(wrappedUri)); // decode %stuff and parse to the proper uri
        }

        startActivity(new Intent(Intent.ACTION_VIEW, cleanedUri)); // broadcast new intent with new URI
    }

    this.finish(); // clear out and close this activity
于 2012-12-28T02:33:20.757 に答える