Вопрос

У меня есть рабочий SMS-приемник, но когда я пытаюсь загрузить другой класс, используя:

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

Я получаю эту ошибку:

Намерение конструктора (SMSRECEIVER, CLASS) не определено

для первой строки и:

Метод StartactivityForresult (намерение, INT) не определен для типа SMSRECEIVER

Для второй линии

Я бы очень ценил несколько советов относительно того, что происходит не так.

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.length; 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);  
    }  
}  
Это было полезно?

Решение

SMSReceiver не является деятельностью. Только мероприятия могут использовать startActivityForResult(), и только Contexts (родительский класс Activity) быть использованным при создании Intent Используя конструктор, который вы выбрали.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top