简体   繁体   English

在类中使用意图而不扩展活动

[英]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. startActivity(i1); //此行正在创建错误,表明此方法未为该smsreceiver类定义此方法,现在的问题看起来像是,是否需要在扩展活动类的类内调用?是强制性的吗?还是可以调用在任何班级和某些班级中,我都不认为这是争论,因此我的时间确实不足,无法满足吐司或意图等某些功能所需的争论。

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. 我的回答会帮助您,然后别忘了支持。 :) :)

use context.startActivity(i1); 使用context.startActivity(i1);

you can use context that passed from Activity class to call another activity 您可以使用从Activity类传递的context来调用另一个活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM