我有一个扩展BroadcastReceiver
的课程。收到短信后,我想将信息传递给我的主要活动类,以便在一个方框中显示文字(如果已有文字,则附加)。
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"));
}
}
我只有一个活动类(MainActivity.java
)。但是,当我这样做时,我得到一个异常无法暂停活动。
答案 0 :(得分:10)
你有三种方式:
1)您可以在MainActivity
内定义广播,如下所示:
在onCreate()
registerReceiver(smsReceiver, new IntentFilter(SMS_RECIEVED));
并在MainActivity
private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//you can update textBox here
handler.postDelayed(sendUpdatesToUI, 10);
}
};
定义可运行以更新UI
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
update();
}
};
和更新方法
private void update(String text) {
textView.setText(textView.getText().toString() + text);
}
2)在Activity和BroadCastReceiver
之间注册一个接收者3)使用新Intent启动Activity以更新当前打开的Activity
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("Key", "text");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
更新:
解释方法2
MainActivity.class
onResume()
中的
registerReceiver(broadcastReceiver, new IntentFilter(SmsReceiver.BROADCAST_ACTION));
onDestroy()
中的
unregisterReceiver(broadcastReceiver);
local broadCast(broadcastReceiver,在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
全局属性
public static final String BROADCAST_ACTION = "your.package.name.displayevent";
private final Handler handler = new Handler();
Intent intent;
Context context;
onReceive()
中的
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI, 10);
this.context = context;//you can retrieve context from onReceive argument
this.intent = new Intent(BROADCAST_ACTION);
定义两个方法
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
display();
}
};
private void display() {
intent.putExtra("key", text);
context.sendBroadcast(intent);
}
答案 1 :(得分:0)
修改您的代码,如下所示。
let test = fun x y u ->
(for i = 0 to (w-1) do
for j = 0 to (h-1) do
if i > x then
if j > y then
tab.(i).(j) = (if u = "DR" then tab.(i).(j) else 0)
else
if j = y then
tab.(i).(j) = (if u = "R" then tab.(i).(j) else 0)
else
tab.(i).(j) = (if u = "UR" then tab.(i).(j) else 0)
else
if i = x then
if j > y then
tab.(i).(j) = (if u = "D" then tab.(i).(j) else 0)
else
tab.(i).(j) = (if u = "U" then tab.(i).(j) else 0)
else
if j > y then
tab.(i).(j) = (if u = "DL" then tab.(i).(j) else 0)
else
if j = y then
tab.(i).(j) = (if u = "L" then tab.(i).(j) else 0)
else
tab.(i).(j) = (if u = "UL" then tab.(i).(j) else 0)
done
done)