Search code examples
javaandroidandroid-broadcastreceiver

No text in Toast


I am trying to display a toast whenever the screen is unlocked. But it shows empty toast(even when I enter some text in EditText). What should I do?

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

Solution

  • I made few mistakes because of which the toast was displaying no text.(I was new to android programming at that point of time)

    What mistakes did I make

    • I did not use Shared Preferences to store my data.
    • Since my receiver was dynamically registered. The toast was only displayed while the app was running or in the background.
    • After I entered the text inside the EditText, I closed my application and removed it from the recent app. Then, I opened the application again which created a new instance of it and the data which I thought would be there was not actually there and there was no text in the toast.

    I would also like to thank Onik for his patience and help.