views:

99

answers:

1

hi ive got a working sms receiver but when i try to load another class using:

Intent intent = new Intent(SMSReceiver.this, SMSNotifier.class);
startActivityForResult(intent, 0);

i get the error:

The constructor Intent(SMSReceiver, Class<SMSNotifier>) is undefined

for the first line and:

The method startActivityForResult(Intent, int) is undefined for the type SMSReceiver

for the second line

id really appreciate some advice as to whats going wrong

thanks, ng93

edit:

package com.prototype.messages;

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.os.Bundle;
import android.telephony.gsm.SmsMessage;

public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
}
// Context context = getApplicationContext();
String ns = Context.NOTIFICATION_SERVICE;
int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
// Intent notificationIntent = new Intent(SMSReceiver.this, Messages.class);
// notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
// PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.setLatestEventInfo(context, contentTitle, contentText, null);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
mNotificationManager.notify(1, notification);
}
}

A: 

SMSReceiver is not an activity. Only activities can use startActivityForResult(), and only Contexts (a parent class of Activity) be used in creating an Intent using the constructor you chose.

CommonsWare
thanks what code should i use instead?
ng93
@ng93: I have no idea. It is your code, not mine. Assuming `SMSReceiver` is a `BroadcastReceiver`, you were passed a `Context` in `onReceieve()` that you could use for creating the `Intent`. However, a `BroadcastReceiver` should *not* be starting an `Activity`, as that will interrupt the user in whatever they are doing.
CommonsWare
i only wanted to start a new activity so it could create a statusbar notification. i did try this in SMSReceiver but couldn't get it to work. Maybe i should be asking how to create a notification in SMSReceiver?
ng93
@ng93: you should be able to create a `Notification` just normally, using the `Context` to get at the `NotificationManager`. I am not aware of any limitations that would prevent this from working.
CommonsWare
ok im getting an undefined error with getSystemService();? p.s. if you hadnt realized already im a noob so sorry for all the questions
ng93
@ng93: First, you need to call `getSystemService()` on the `Context`, as I noted in my previous comment. Your `Context` is the first parameter to `onReceive()`. Second, you cannot implement an SMS receiver except by using things that are not part of the SDK, so your receiver may break in the future (or even possibly on some of today's devices).
CommonsWare
ok will do that cheers
ng93