1

我正在使用 SMSBroadcastReceiver 类来收到一条短信,它会在我的日历上创建一个事件,代码是这样的

package com.arnaldo.smscal;

    import java.util.GregorianCalendar;

    import android.content.BroadcastReceiver;
    import android.content.ContentResolver;
    import android.content.ContentUris;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.Contacts;
import android.provider.CalendarContract.Events;
import android.provider.Contacts.People;
import android.provider.Contacts.Phones;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


public class SMSBroadcastReceiver extends BroadcastReceiver{



    private static final ContentResolver ContentResolver = null;
    private static final Context Context = null;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();           //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;




            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    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]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                        Log.e(msg_from, msgBody);


        String contact;
                        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(msg_from));  
                        Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+msg_from+"'",null,null);

                        if(cs.getCount()>0)
                        {
                         cs.moveToFirst();
                         contact=cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
                         Log.e("Foi de", contact);


                         //------------//

                         Intent calIntent = new Intent(Intent.ACTION_INSERT); 
                         calIntent.setType("vnd.android.cursor.item/event");    
                         calIntent.putExtra(Events.TITLE, "My House Party"); 
                         calIntent.putExtra(Events.EVENT_LOCATION, "My Beach House"); 
                         calIntent.putExtra(Events.DESCRIPTION, "A Pig Roast on the Beach"); 

                         GregorianCalendar calDate = new GregorianCalendar(2012, 9, 5);
                         calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true); 
                         calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, calDate.getTimeInMillis()); 
                         calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, calDate.getTimeInMillis()); 

                         startActivity(calIntent);  


                         //------------//


                        } 


                    }
                }catch(Exception e){
                           Log.w("Exception caught",e.getMessage());
                }
            }
        }
    }
}

问题是 startActivity(calIntent);
我有错误“未定义 SMSBroadcastReceiver 类型的方法 startActivity(Intent)”

请帮忙

4

1 回答 1

0

使用上下文,因为它被传递到 onReceive 函数中:

      context.startActivity(intent);
于 2013-12-10T15:36:00.813 回答