Calling Status Bar notification from method from other class.
- by Jez Fischer
Firstly, I am new to both android and Java.
I have two classes, my main.class and Note.class.
I am calling the notification method from my Note.class in my main.class when i press a button.
The issue is with this line from the Note.class :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
When the method is called it force closes. I believe the problem to be with the "this" in PendingIntent.getActivity(this, 0, notificationIntent, 0);, but I am unsure what to change it to.
The notification code works fine if it's in the main class.
I would be very grateful for any guidance.
Edit:
Main class : http://pastebin.com/05Yx0a48
Note.class :
package com.adamblanchard.remindme.com.adamblanchard;
import com.adamblanchard.remindme.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class Note extends Activity {
public CharSequence note = "not changed";
int HELLO_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setTitle("Remind Me!");
}
//Notification Method
public void callNotification() {
// TODO Auto-generated method stub
String ns = Context.NOTIFICATION_SERVICE;
final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.launcher;
CharSequence tickerText = "Remind Me!";
long when = System.currentTimeMillis();
final Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
final Context context = getApplicationContext();
CharSequence contentTitle = "Remind Me!";
CharSequence contentText = note;
Intent notificationIntent = new Intent(context, AndroidNotifications.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
HELLO_ID++;
}
}
Debug Output :
Thread [<1 main] (Suspended (exception IllegalStateException))
Note(Activity).getSystemService(String) line: 3536
Note.callNotification() line: 37
remindme$1$1.onClick(DialogInterface, int) line: 72
AlertDialog(AlertController$ButtonHandler).handleMessage(Message) line: 159
AlertController$ButtonHandler(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3647
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]
This is the debug output I get, plus a force close popup on the device.
Edit2:
Manifest xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.adamblanchard.remindme"
android:versionCode="3"
android:versionName="0.7">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher72">
<activity android:name=".com.adamblanchard.remindme" 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=".Note">
<intent-filter>
<action android:name="Note" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="1"></uses-sdk>
</manifest>
Stack traces (Are these what you mean?):
Thread [<1> main] (Suspended (exception ActivityNotFoundException))
Instrumentation.checkStartActivityResult(int, Object) line: 1404
Instrumentation.execStartActivity(Context, IBinder, IBinder, Activity, Intent, int) line: 1378
remindme(Activity).startActivityForResult(Intent, int) line: 2827
remindme(Activity).startActivity(Intent) line: 2933
remindme$1$1.onClick(DialogInterface, int) line: 82
AlertDialog(AlertController$ButtonHandler).handleMessage(Message) line: 159
AlertController$ButtonHandler(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 3647
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]