Search This Blog

Showing posts with label notification. Show all posts
Showing posts with label notification. Show all posts

31 May 2010

Disclaimer


Use the Alert Dialogs example as a starting point.....

Add this icon to your src\main\android\res\drawable directory

I grabbed that icon from the Tango project, but had to change the filename to be compatable with Android.

Then from the Google Analytics example, create an analytics profile, update your pom.xml and AndroidManifest.xml


Instead of putting the tracker code directly into the onCreate method, let's move that into the "I Accept!" button.  Note the change to onDestroy as well!

public class DisclaimerTestActivity extends Activity {
    private static String TAG = "DisclaimerTest";
    private GoogleAnalyticsTracker tracker = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog dialog = new AlertDialog.Builder(this).create();
        dialog.setTitle("Disclaimer");
        dialog.setMessage("In order to provide support for this application, we reserve the right to anonymously track and report usage information in this application.  We will track which parts of the application are popular but will not track who you are or what you type / look at.");
        dialog.setIcon(R.drawable.network);
        dialog.setButton("I Accept!", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                tracker = GoogleAnalyticsTracker.getInstance();
                tracker.start("UA-YOUR-ACCOUNT-HERE", DisclaimerTestActivity.this);
                setContentView(R.layout.main);
                track("/");
                dispatch();
                return;
            }
        });
        dialog.setButton2("No Way!", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int i) {
                finish();
                return;
            }
        });
        dialog.show();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        if(tracker != null) tracker.stop();
    }

    // track("Clicks", "Button", "clicked", 77
    private void track(String category, String action, String label, int value)
    {
        tracker.trackEvent(category, action, label, value);
    }

    // track("/testApplicationHomeScreen");
    // track("/download");
    private void track(String page)
    {
        tracker.trackPageView(page);
    }

    // Allows sending in bulk by queuing up multiple tracks before calling dispatch
    // timestamps based on when dispatch is called
    private void dispatch()
    {
        tracker.dispatch();
    }
}


Redeploy (mvn clean install).  Now, if you accept the disclaimer it will do analytic notifications (which can take up to 24 hours to show up).  If you cancel, it will just close the app.

23 April 2010

Loading, please wait...

This time we are going to show the standard Please Wait dialog with the spinning circle.  This example is taken from the developer documentation.  Using any of our examples as a starting point, let's replace the onCreate method in MyActivity.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "", "Loading. Please wait...", true);
        Thread t = new Thread(new Runnable(){
            public void run() {
                try{
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // no-op
                }
                dialog.dismiss();
            }
        });
        t.start();
    }

Redeploy (mvn clean install) and the dialog will show for 5 seconds before going away.

List picker and fading popup notice

This example can be created from any of the previous as we are just going to replace the content of MyActivity.  In this case, we are going to create a popup, allow the user to choose one, and then show a temporary message telling them what they chose.  This example is directly from the developer documentation.

Open MyActivity.java and replace the contents of the onCreate method with the following:


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final CharSequence[] items = {"Red", "Green", "Blue"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Pick a color");
        builder.setItems(items, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                return;
            }
        });
        builder.create().show();
    }

Once you redeploy (mvn clean install) and launch your app, you will see the dialog.  Choose one and see the popup.

Notifications

Building on our Alert example, we are going to take a look at the Notification Service.  We are basing this example on the developer documentation.

First, open your MyActivity.java.  In the top of the file, add a static id for our notification:
    private static final int HELLO_ID = 1;
In the onClick method of our alert, replace the:
return;
with:
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mgr = (NotificationManager) getSystemService(ns);
               
                int icon = R.drawable.icon;
                CharSequence tickerText = "You closed the alert";
                long when = System.currentTimeMillis();
                Notification notification = new Notification(icon, tickerText, when);

                Context context = getApplicationContext();
                CharSequence contentTitle = "Hello Notification";
                CharSequence contentText = "This notification is to let you know you closed the popup alert.";
                Intent notificationIntent = new Intent(context, MyActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

                mgr.notify(HELLO_ID, notification);

Redeploy (mvn clean install) your app.  When you click OK to close the alert, a notification will appear in the notification bar.