Case study: I need a program that will add a notification at android. And when someone click on the notification it should lead them to my second activity.
I have established a code. It should be working. Dont know what am i missing.
My code structure.
java files:
MainActivity.java
NotificationReceiverActivity.java
Layout file:
activity_main.xml
result.xml
Code of those files:
MainActivity.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
String longText = "Hello Hello Hello";
Notification n = new Notification.Builder(this)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentText("Subject")
.setContentIntent(pIntent).setAutoCancel(true)
.setStyle(new Notification.BigTextStyle().bigText(longText))
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
notificationManager.notify(0, n);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
NotificationReceiverActivity.java
public class NotificationReceiverActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
}
}
activity_main.xml
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:id="@+id/button1" android:layout_height="match_parent"
android:layout_width="match_parent">
result.xml
android:layout_width="match_parent" android:layout_height="match_parent"> android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/textView1">
AndroidManifest.xml
package="com.example.notification"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="16"
android:targetSdkVersion="15" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/title_activity_main" >
.
stackoverflow.comm
No comments:
Post a Comment