启动活动里面的onReceive的BroadcastReceiver [英] Start Activity inside onReceive BroadcastReceiver

查看:128
本文介绍了启动活动里面的onReceive的BroadcastReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始一个活动在我的的onReceive()方法。

 包com.splashscreenactivity;

进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.telephony.SmsMessage;
进口android.widget.Toast;

公共类SMSReceiver扩展的BroadcastReceiver {

    公共静态字符串trigger_message =;

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        // ---得到通过短信---
        捆绑包= intent.getExtras();
        SmsMessage []封邮件= NULL;
        字符串str =;
        如果(捆绑!= NULL){
            // ---检索收到的短信---
            [对象]的PDU =(对象[])bundle.get(的PDU);
            封邮件=新SmsMessage [pdus.length]
            的for(int i = 0; I< msgs.length;我++){
                封邮件[I] = SmsMessage.createFromPdu((字节[])的PDU [I]);
                STR + =短信来自+封邮件[I] .getOriginatingAddress();
                STR + =;
                。trigger_message =封邮件[I] .getMessageBody()的toString();
                STR + = trigger_message;
                STR + =\ N的;
            }
            // ---显示新短信---
            Toast.makeText(背景下,海峡,Toast.LENGTH_SHORT).show();
            如果(trigger_message.equals(DX)){
                Toast.makeText(背景下,我引发的,Toast.LENGTH_LONG)
                        。显示();
                // /////////////////////////
                //我想从这里开始
                // ////////////////////////
                // MainScreenActivity.trigger =现在;
                //意图I =新的意图(背景下,GPS.class);
                // i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                // context.startActivity(ⅰ);
            } 其他 {
                Toast.makeText(背景下,我没有触发,Bbyz!,
                        Toast.LENGTH_LONG).show();
            }
        }
    }
}
 

下面是GPS.class

 包com.splashscreenactivity;

进口android.app.Activity;
进口android.content.BroadcastReceiver;
进口android.content.Context;
进口android.content.Intent;
进口android.location.Location;
进口android.location.LocationListener;
进口android.location.LocationManager;
进口android.os.Bundle;
进口android.telephony.SmsManager;
进口android.telephony.SmsMessage;
进口android.widget.TextView;
进口android.widget.Toast;

公共类GPS扩展活动实现LocationListener的{

    TextView的纬度,logitude;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.gps);
        LocationManager LM =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
                本);
        Toast.makeText(这一点,我现在开始,Toast.LENGTH_LONG);
        //纬度=(TextView中)findViewById(R.id.txtLat);
        // logitude =(TextView中)findViewById(R.id.txtLongi);
        // latitude.setText(载入中...);
        // logitude.setText(载入中...);
    }

    字符串LATTITUDE;
    字符串LOGITUDE;

    @覆盖
    公共无效onLocationChanged(位置定位){
        双纬度= location.getLatitude();
        双滞后= location.getLongitude();
        LATTITUDE = Double.toString(LAT);
        LOGITUDE = Double.toString(滞后);
        // latitude.setText(LATTITUDE);
        // logitude.setText(LOGITUDE);
        // SmsManager的SM = SmsManager.getDefault();
        // //此处就是文本的目的应该去
        //串号=5556;
        // sm.sendTextMessage(数字,空,
        //纬度=+ latitude.getText()+\ nlongitude =+ logitude.getText(),
        // NULL,NULL);
    }

    @覆盖
    公共无效onProviderDisabled(字符串为arg0){
    }

    @覆盖
    公共无效onProviderEnabled(字符串为arg0){
    }

    @覆盖
    公共无效onStatusChanged(字符串为arg0,INT ARG1,捆绑ARG2){
    }
    // / **注册为更新时,活动是在前台* /
    // @覆盖
    //保护无效onResume()
    // {
    // super.onResume();
    // lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000L,500.0f,
    // 本);
    //}
    //
    // / **停止更新活动时暂停* /
    // @覆盖
    //保护无效的onPause(){
    // super.onPause();
    // lm.removeUpdates(本);
    //}
}
 

解决方案

您有上下文作为参数传递给onRecieve()方法,因此就使用:

  @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        //启动活动
        意图I =新意图();
        i.setClassName(com.test,com.test.MainActivity);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(ⅰ);
    }
 

它的工作原理,当然你必须改变包装和活动类名到您自己的。

I want to start an activity in my onReceive() method.

package com.splashscreenactivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {

    public static String trigger_message = "";

    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        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 += " :";
                trigger_message = msgs[i].getMessageBody().toString();
                str += trigger_message;
                str += "\n";
            }
            // ---display the new SMS message---
            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            if (trigger_message.equals("dx")) {
                Toast.makeText(context, "I am triggered", Toast.LENGTH_LONG)
                        .show();
                // /////////////////////////
                // i want to start here
                // ////////////////////////
                // MainScreenActivity.trigger="Now";
                // Intent i = new Intent(context,GPS.class);
                // i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                // context.startActivity(i);
            } else {
                Toast.makeText(context, "I am not triggered,  Bbyz!!!",
                        Toast.LENGTH_LONG).show();
            }
        }
    }
}

here is GPS.class

package com.splashscreenactivity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.TextView;
import android.widget.Toast;

public class GPS extends Activity implements LocationListener {

    TextView latitude, logitude;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gps);
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f,
                this);
        Toast.makeText(this, "i m started", Toast.LENGTH_LONG);
        // latitude = (TextView)findViewById(R.id.txtLat);
        // logitude = (TextView)findViewById(R.id.txtLongi);
        // latitude.setText("Loading...");
        // logitude.setText("Loading...");
    }

    String LATTITUDE;
    String LOGITUDE;

    @Override
    public void onLocationChanged(Location location) {
        double lat = location.getLatitude();
        double lag = location.getLongitude();
        LATTITUDE = Double.toString(lat);
        LOGITUDE = Double.toString(lag);
        // latitude.setText(LATTITUDE);
        // logitude.setText(LOGITUDE);
        // SmsManager sm = SmsManager.getDefault();
        // // here is where the destination of the text should go
        // String number = "5556";
        // sm.sendTextMessage(number, null,
        // "latitude="+latitude.getText()+"\nlongitude="+logitude.getText(),
        // null, null);
    }

    @Override
    public void onProviderDisabled(String arg0) {
    }

    @Override
    public void onProviderEnabled(String arg0) {
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    }
    // /** Register for the updates when Activity is in foreground */
    // @Override
    // protected void onResume()
    // {
    // super.onResume();
    // lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f,
    // this);
    // }
    //
    // /** Stop the updates when Activity is paused */
    // @Override
    // protected void onPause() {
    // super.onPause();
    // lm.removeUpdates(this);
    // }
}

解决方案

You have context passed as parameter to onRecieve() method, so just use:

 @Override
    public void onReceive(Context context, Intent intent) {
        //start activity
        Intent i = new Intent();
        i.setClassName("com.test", "com.test.MainActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }

It works, of course you have to change package and activity class name to your own.

这篇关于启动活动里面的onReceive的BroadcastReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆