views:

183

answers:

1

Hi, I'm new to android... having trouble trying to open a URL from a live wallpaper..

Code to open the url:

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));

            Activity activity = new Activity();
            activity.startActivity(intent);

But I just get a NullpointerException:

 java.lang.NullPointerException
     at android.app.Activity.startActivityForResult(Activity.java:2749)
     at android.app.Activity.startActivity(Activity.java:2855)
     at org.amble.graphics.HToolbar$URLEntry.run(HToolbar.java:81)
     at org.amble.graphics.HToolbar.doTouch(HToolbar.java:188)
     at com.mobstar.undeadpirate.autopaper.PiratePaperPainter.doTouchEvent(PiratePaperPainter.java:245)
     at com.mobstar.undeadpirate.autopaper.LiveWallpaperService$WallpaperEngine.onTouchEvent(LiveWallpaperService.java:117)
     at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:832)
     at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:45)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at android.os.Looper.loop(Looper.java:123)
     at android.app.ActivityThread.main(ActivityThread.java:4363)
     at java.lang.reflect.Method.invokeNative(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:521)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
     at dalvik.system.NativeStart.main(Native Method)

My manifest looks like this

<?xml version="1.0" encoding="utf-8"?>
<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1"
    android:versionName="1.0" package="com.nubjub.wallpapertest">
    <application 
        android:label="@string/app_name" android:debuggable="true" android:icon="@drawable/icon">
        <service 
            android:name="WallpaperTest"
            android:enabled="true"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:permission="android.permission.BIND_WALLPAPER">
            <intent-filter android:priority="1" >
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data 
                android:name="android.service.wallpaper" 
                android:resource="@xml/wallpaper" />
        </service>
    </application>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="7" />
</manifest> 
+2  A: 

Of course you can open a Browser from a Live Wallpaper! The required code is:

Intent myIntent = new Intent(Intent.ACTION_VIEW,
                             Uri.parse("http://www.example.com"));
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(myIntent);

The only thing you need to set is that the live wallpaper should be able to start a new task which we do via the Intent.setFlags(int flag) line.

You don't need to instantiate a new activity, I suggest you read up on the android activity life-cycle and exactly what intents do!

BeRecursive
OP, note that nowhere in this code did he say `new Activity().` In Android, the operating system will (99% of the time) be the one creating a component (an `Activity`, `BroadcastReceiver`, `Service`, etc). If you find yourself saying `new <component>` then you are likely doing it wrong :)
Hamy
Cool, this was my first android anything... so I knew nothing. I'll give this a go and award the all important ✔ if it works tomorrow.
Stuart Axon
OK, ticking... turns out that I need to rearrange my code a bit, so I can call startActivity from the service.
Stuart Axon