Friday 29 June 2012

Android sending message to a Handler on a dead thread AsyncTask

When developing your Android app and you start mixing AsyncTask with BroadcastReceiver, you might see the reports of "sending a message to a Handler on a dead thread"

Having repeatedly checking my code and not finding a fault I discovered after much research the following.

AsyncTask expects to be initialised on the Main thread. This might not happen when you are using a BroadcastReceeiver.

A workaround is to ensure that AsyncTask is initialised in your main application like this:

public class YourApplication extends Application {

    @Override
    public void onCreate() {

        // workaround for http://code.google.com/p/android/issues/detail?id=20915
        try {
            Class.forName("android.os.AsyncTask");
        } catch (ClassNotFoundException e) {
        }

        super.onCreate();
    }
} 
 
and define YourApplication in your Manifest.

Friday 22 June 2012

Silverlight Elevated Trust in Browser

So you want to develop and deploy a Silverlight 5 application that has elevated permissions but runs in-browser, so you can access file system and use DLLs and all those other things a web app shouldn't really be doing. Well here is what you need to do:

1) First go to the properties of your Silverlight app and the Silverlight tab to ensure the "Required Elevated trust when running in browser" is enabled. You don't need the out of browser check box ticked.
  

2)  Next go to the Signing tab and choose a certificate or create a test certificate if you don't have an existing certificate and then tick the Sign the Xap File. 

 
3) Export and save the certificate you have just created, you'll need this later for deployment. Do this by clicking More Details, then on the Details tab choose Copy to File. Follow the wizard and select export private key and save the .PFX file. Don't forget to use a password you will remember when prompted.
 
4) Now build your app and copy the following files from the associated web project of your Silverlight app to your web server. This can be IIS or Apache, so either the wwwroot folder for IIS or htdocs folder for Apache:
a) the html page hosting your Silverlight app
b) Silverlight.js 
c) the ClientBin folder and its contents which is your xap file.

 

5) Now we are already at the server end, time to move to the client which can be the same machine. 

To enable Elevated Trust you need to modify the following settings in the registry of the client machine where the Silverlight runtime is installed. All the details are here http://msdn.microsoft.com/en-us/library/gg192793%28v=vs.95%29.aspx but the key bit is below:
 

6)  If you are using IE now is a good time to add your site to the list of Trusted sites. You need to do this because even though the Silverlight app is granted elevated permissions, IE will run in a protected mode and prevent access to the file system etc. The app can end up in a partial trusted state where it thinks it has Elevated Trust but some operations will still fail. This doesn't seem to be necessary with Firefox.

 
7) The final step is to install your test certificate that your saved early to the .PFX file.

This needs to be installed in the Trusted Publishers Store. This link describes deploying certificates, especially if you are using a test certificate.  Make sure you follow all the steps, especially no.12 if your certificate is self-signed as a test certificate will be.

8) You should be able to browse to your host page and see your app run with elevated permissions. 

Your app can check to see whether Elevated Trust is available with the call:
Application.Current.HasElevatedPermissions

9) To host Silverlight apps in Apache you just need to add the following entries to the mime.types file in the Apache2.2\Conf folder:
application/xaml+xml                xaml
application/x-msdownload            dll
application/x-silverlight-app       xap