在不同的班级创建

时间:2016-02-09 09:54:38

标签: java android

package dgameman1.com.emojiupdaterroot;

import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;

import java.io.IOException;
public class DownloadBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
        alertDialog.setTitle("Restart?");
        alertDialog.setMessage("Android needs to restart to update Emojis.");
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "I don't have a choice",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            Runtime.getRuntime().exec("su -c reboot");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
        alertDialog.show();



    }
}
}

所以出于某种原因,这一行

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();

MainActivity.this下面有一条红色的波浪线我不知道该怎么改变它,因为当它放在我的MainActivity.Java中时它才起作用而不是当我把它放在BroadCastReceiver.java类中时

1 个答案:

答案 0 :(得分:0)

问题是你试图从BroadcastReceiver显示一个AlertDialog,这是不允许的。您无法从BroadcastReceiver显示AlertDialog。您可以为您的dialogBox创建一个活动,并在BroadcastReceiver中启动您的活动。或者在Activity中创建您的Broadcast类,然后调用您的Activity的方法。 请参阅此示例show an alert dialog in broadcast receiver after a system reboot

示例:

用于网络的broadCastReceiver更改状态,使用dialogBox启动活动:

public class NetworkStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {


        Log.d("app", "Network connectivity change");
        if (intent.getExtras() != null) {
            NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
                Log.i("app", "Network " + ni.getTypeName() + " connected");
                Toast.makeText(context, context.getResources().getString(R.string.network_on), Toast.LENGTH_LONG).show();
                Intent intent1 = new Intent(context , Main2Activity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);

            } else if (intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, Boolean.FALSE)) {
                Log.d("app", "There's no network connectivity");
                Intent intent1 = new Intent(context , Main2Activity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
                Toast.makeText(context, context.getResources().getString(R.string.network_off), Toast.LENGTH_LONG).show();
            }
        }


    }
}

actvity的代码:

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        dialogbox();
    }


    public void dialogbox(){

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        // // set the message to display
        alertbox.setTitle("title");
        //alertbox.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        // alertbox.setIcon(R.drawable.ic_launcher);

        LayoutInflater factory = LayoutInflater.from(this);
        //View layout = factory.inflate(R.layout.dialog_box, null);

        //alertbox.setView(layout);
        alertbox.setNeutralButton("cancel",
                new DialogInterface.OnClickListener() {

                    // click listener on the alert box
                    public void onClick(DialogInterface arg0, int arg1) {
                        // the button was clicked
                        Main2Activity.this.finish();
                    }
                });
        // show it
        alertbox.show();


    }

}

不要忘记在广播接收器中添加标志新任务,并通过将其添加到清单来注册广播接收器:

<receiver android:name=".NetworkStateReceiver">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
            </intent-filter>
        </receiver>