SQLDoctor
SQLDoctor

Reputation: 353

using intent in class which doesn't extend activity

guys been bit confused with this.I'm using intent from one activity to another n that is working perfectly, here is the code which is creating a problem

    package com.furniture;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.util.Log;
    import android.widget.Toast;


    public class smsreciever extends BroadcastReceiver
    {

        public String value;    
        @Override
        public void onReceive(Context context, Intent intent) 
        {

            //---get the SMS message passed in---
            Bundle bundle = intent.getExtras();        
            SmsMessage[] msgs = null;
            String str = "";
            String info="";
            if (bundle != null)
            {
                //---retrieve the SMS message received---
                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();
                    info=msgs[i].getMessageBody().toString();
                    str += "\n";        
                }
                //---display the new SMS message---
    //        Intent i1=new Intent();
                //((Gloabal)this.get).setData(str);
                Gloabal.setData(info);
                //Gloabal g1=new Gloabal();
                //Global g = (Global)getApplication();
                //int data=g.getData();
                //String temp=Gloabal.getData();

                Log.v("sanketh","smsreciver value of str:"+str);
                //int a=1;
                //Log.v("sanketh","global value :"+temp);
                //g1.setData(str);
                Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
                String ref=Gloabal.getData();
                if(ref!=null)
                {


                Log.v("sanketh","value of refer="+ref);

                if(ref.equals(info))
                {
                    Log.v("sanketh","value =1");
                    Intent i1=new Intent(context,login.class);
                    i1.putExtra("confirm", info);
                //  startActivityForResult(i1, 0);
                    startActivity(i1);
                    //Toast.makeText(Context, "mathces", 5).show();
                }else
                {
                    Log.v("sanketh","value =2");
                    //Toast.makeText(con, "doesnot match", 5).show();

                }
                }
            }                         
        }
    }

startActivity(i1);//this lines is creating error saying this method is undefined for this smsreceiver class now the problem looks like ,is it required to call inside a class which extends activity class?is it mandatory to that?or can we call it in any class,and in some class i do not have view as the arguement so that time i do fall short satisfying the arguement required for some functions like toast or intent.

Upvotes: 1

Views: 873

Answers (2)

Satyaki Mukherjee
Satyaki Mukherjee

Reputation: 2879

It's very simple my friend. Just use this intent in this manner:

Intent i1 = new Intent(context, login.class);  
i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i1);

I think you will able to solve the problem.

Enjoy!!

I my answer will help you then don't forget to support. :)

Upvotes: 3

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

use context.startActivity(i1);

you can use context that passed from Activity class to call another activity

Upvotes: 1

Related Questions