2011-06-24 42 views
58

Tôi muốn bắt đầu một hoạt động trong phương pháp onReceive() của mình.Bắt đầu hoạt động bên trong onReceive BroadcastReceiver

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(); 
      } 
     } 
    } 
} 

đây là 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); 
    // } 
} 
+1

gì là vấn đề bạn đang nhận được? –

+0

Bạn đã có mã bắt đầu hoạt động. Chỉ cần bỏ ghi chú. – jamapag

+1

Trên thực tế phương thức startActivity không hoạt động trong phương thức nhận vì lớp này không mở rộng Hoạt động –

Trả lời

130

Bạn đã bối cảnh truyền như tham số để phương pháp onRecieve(), vì vậy chỉ cần sử dụng:

@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); 
    } 

Nó hoạt động, tất nhiên bạn phải thay đổi tên gói và hoạt động thành tên của riêng bạn.

+0

sry tôi đã không nhận được nó có thể u pls xây dựng !! –

+0

hy vọng, đó là rõ ràng – piotrpo

+0

@piotrpo nếu im đăng ký nhận phát sóng của tôi từ biểu hiện sau đó tôi có thể bắt đầu hoạt động từ lớp reciever phát sóng bằng cách sử dụng context.getApplicationContext.startActivity() nhưng ứng dụng của tôi bị rơi – user3233280

26

Tôi đang sử dụng này và công việc của mình trên trang web của tôi:

Intent intentone = new Intent(context.getApplicationContext(), DialogAct.class); 
intentone.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(intentone); 
+0

Nó không hoạt động ... hoạt động mới được khởi chạy với ngăn xếp hoạt động khác. Hoạt động ngăn xếp không nhận được rõ ràng .. khác đề nghị khác! – CoDe

+0

làm việc tốt, tôi đã thử nghiệm nó trong ứng dụng của tôi –

0
Intent intent1 = new Intent(); 
intent1.setClassName(context.getPackageName(), MainActivity.class.getName()); 
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(intent1); 

để ngăn chặn những trường hợp ngoại lệ gói

Các vấn đề liên quan