2015-06-14 17 views
5

Tôi có một lớp học mở rộng BroadcastReceiver. Khi nhận được một tin nhắn SMS, tôi muốn chuyển thông tin đến lớp hoạt động chính của tôi để hiển thị văn bản trong một hộp (Thêm, nếu đã có văn bản).Gửi mục đích từ lớp BroadcastReceiver đến hoạt động hiện đang hoạt động

public class SmsReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) 
    { 
     Intent i = new Intent(context, MainActivity.class); 
      i.putExtra("updatedString","Hello"); 
      context.startActivity(i); 
    } 
} 

MainActivity.java

public class MainActivity extends Activity{ 

    private TextView results; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Bundle extras = getIntent().getExtras(); 
     if(extras!=null){ 
      results = (TextView) findViewById(R.id.results); 
      results.setVisibility(View.VISIBLE); 
      results.append(extras.getString("updatedString")); 
     } 

} 

Tôi chỉ có một lớp hoạt động (MainActivity.java). Tuy nhiên Khi tôi làm điều này tôi nhận được một ngoại lệ Không thể tạm dừng Hoạt động.

Trả lời

8

Bạn có ba cách:
1) Bạn có thể xác định phát sóng của bạn bên trong MainActivity của bạn như thế này:
trong onCreate()

registerReceiver(smsReceiver, new IntentFilter(SMS_RECIEVED)); 

và xác định smsReciver trong MainActivity

private BroadcastReceiver smsReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     //you can update textBox here 
     handler.postDelayed(sendUpdatesToUI, 10); 
    } 
}; 

định nghĩa một Runnable để cập nhật giao diện người dùng

private Runnable sendUpdatesToUI = new Runnable() { 
    public void run() { 
     update(); 
    } 
}; 

và phương pháp cập nhật

private void update(String text) { 
    textView.setText(textView.getText().toString() + text); 
} 

2) Đăng ký một máy thu giữa hoạt động của bạn và BroadCastReceiver

3) Bắt đầu hoạt động của bạn với Ý định mới để cập nhật Hoạt động mở hiện

Intent intent = new Intent(context, MainActivity.class); 
intent.putExtra("Key", "text"); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(intent); 

CẬP NHẬT :
phương pháp giải thích 2
MainActivity.class

trong onResume()

registerReceiver(broadcastReceiver, new IntentFilter(SmsReceiver.BROADCAST_ACTION)); 

trong onDestroy() Broadcast

unregisterReceiver(broadcastReceiver); 

địa phương (broadcastReceiver, trong MainActivity.class)

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     updateUI(intent); 
    } 
}; 
private void updateUI(Intent intent) { 
    String text = intent.getStringExtra("key"); 
    textView.setText(textView.getText().toString() + text); 
} 

SmsReceiver.class
toàn cầu thuộc tính

public static final String BROADCAST_ACTION = "your.package.name.displayevent"; 
private final Handler handler = new Handler(); 
Intent intent; 
Context context; 

trong onReceive()

handler.removeCallbacks(sendUpdatesToUI); 
handler.postDelayed(sendUpdatesToUI, 10); 

this.context = context;//you can retrieve context from onReceive argument 

this.intent = new Intent(BROADCAST_ACTION); 

xác định hai phương pháp

private Runnable sendUpdatesToUI = new Runnable() { 
    public void run() { 
     display(); 
    } 
}; 

private void display() { 
    intent.putExtra("key", text); 
    context.sendBroadcast(intent); 
} 
+0

Làm cách nào để gửi tin nhắn từ Lớp SmsReceiver.Tôi đã viết mã sau đây: Intent i = new Intent ("SMS_RECIEVED"); i.putExtra ("updatedString", str); LocalBroadcastManager mgr = LocalBroadcastManager.getInstance (ngữ cảnh); mgr.sendBroadcast (i); – user1692342

+0

theo cách nào bạn muốn sử dụng? 1 hoặc 2 hoặc 3? – MHP

+0

Tôi đang thử phương pháp 1 – user1692342

0

Sửa đổi mã của bạn như dưới đây.

public class SmsReceiver extends BroadcastReceiver { 
@Override 

public void onReceive(Context context, Intent intent) 
    { 
     Intent i = new Intent(context, MainActivity.class); 
      i.putExtra("updatedString","Hello");   i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
      context.startActivity(i); 
    } 
} 

public class MainActivity extends Activity{ 

    private TextView results; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Bundle extras = getIntent().getExtras(); 
     if(extras!=null){ 
      results = (TextView) findViewById(R.id.results); 
      results.setVisibility(View.VISIBLE); 
      results.append(extras.getString("updatedString")); 
     } 

@Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 
     //handle your intent here.Note this will be called even when activity first created.so becareful to handle intents correctly. 
    } 

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