0
我試圖在解鎖屏幕時顯示吐司。但它顯示空吐司(即使我在EditText中輸入一些文本)。我該怎麼辦?吐司中沒有文本
package com.example.hpi5.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button button = null;
MyReceiver receiver = null;
EditText editText = null;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new MyReceiver();
editText = (EditText) findViewById(R.id.editText);
text = editText.getText().toString();
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setAction("my.action.string");
intent.putExtra("str",text);
sendBroadcast(intent);
}
});
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_PRESENT);
filter.addAction("my.action.string");
this.registerReceiver(receiver, filter);
}
public class MyReceiver extends BroadcastReceiver
{
private String te ;
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("my.action.string")) {
te = intent.getExtras().getString("str");
}
if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Toast.makeText(context,te , Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onDestroy()
{
if(receiver!=null)
this.unregisterReceiver(receiver);
}
}
評論是不適合擴展討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/129751/discussion-on-question-by-rayan-i-am-trying-to-display-a-toast-whenever-在-SCRE)。 –
據我所知,該過程可能會死亡,但根據文章應用程序重新啓動,因爲它是以前。 – rayan