Friday, June 21, 2013

Start activity once notification clicked

In the exercise "Generate Notification with sound when alarm received", a browse opened and load a specified url once user click on the notification. We can also pass intent to start activity to notification, to start activity once user click on the notification.

Start activity once notification clicked

Modify AlarmReceiver.java in the exercise "Generate Notification with sound when alarm received", pass Intent(context, DoSomething.class) to PendingIntent.getActivity(...).
package com.example.androiddatepicker;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {
 
 private static final int MY_NOTIFICATION_ID=1;
 NotificationManager notificationManager;
 Notification myNotification;

 @Override
 public void onReceive(Context context, Intent intent) {
  Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();

     Intent myIntent = new Intent(context, DoSomething.class);
     PendingIntent pendingIntent = PendingIntent.getActivity(
            context, 
            0, 
            myIntent, 
            Intent.FLAG_ACTIVITY_NEW_TASK);
     
     myNotification = new NotificationCompat.Builder(context)
       .setContentTitle("Exercise of Notification!")
       .setContentText("Do Something...")
       .setTicker("Notification!")
       .setWhen(System.currentTimeMillis())
       .setContentIntent(pendingIntent)
       .setDefaults(Notification.DEFAULT_SOUND)
       .setAutoCancel(true)
       .setSmallIcon(R.drawable.ic_launcher)
       .build();
     
     notificationManager = 
       (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
     notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
 }

}


Create DoSomething.java, it will be started once user click the notification.
package com.example.androiddatepicker;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

public class DoSomething extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  ImageView image = new ImageView(this);
  image.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
  setContentView(image);
  Toast.makeText(getApplicationContext(), 
    "Do Something NOW", 
    Toast.LENGTH_LONG).show();
 }

}


Modify AndroidManifest.xml to add <activity> of "DoSomething".
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androiddatepicker"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androiddatepicker.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.androiddatepicker.DoSomething"
            android:label="@string/app_name" >
        </activity>
        <receiver android:name=".AlarmReceiver" android:process=":remote" />
    </application>

</manifest>


download filesDownload the files.

5 comments:

  1. sir i am getting error in AlarmReceiver while passing context

    ReplyDelete
  2. thank you very much :D

    ReplyDelete
  3. How to make snooze button and stop alarm button ?

    ReplyDelete
  4. Thanks for the nice tutorial!. Only a question, every works fine in debug mode, but when I export to distribution mode (production) the app doesn open when the notification is selected, ¿Any idea? I hope you can help me. Thank you in advance.

    ReplyDelete
  5. Sir, I am creating reminder application in android studio. I want to store the task with particular date and time in SQ LITE and I want to show it in ActivityMain.xml.Is there any source code for for this activity. Please, post some source code for this.

    Thank you in advance!!!

    ReplyDelete