没有从通知回来,Broadcastreceiver没有工作

时间:2017-03-07 08:22:24

标签: android

我的目标是

  1. 按下按钮btnAktivieren
  2. 然后在一段时间后(随机定义)显示通知。
  3. 我点按此通知,这会将我引导至alertdialog
  4. 在做出AlertDialog选择后,它会回到2。
  5. 到目前为止,即使代码应该贯穿整个

    ,我也只能达到3

    我尝试了什么

    MainActivity.java

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    import java.util.Random;
    
    public class MainActivity extends AppCompatActivity {
    
        boolean aktiviert = false;
        public EditText minutenVon = null;
        public EditText minutenBis = null;
        public Context con = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            con = this;
    
            final Button btnAktivieren = (Button)findViewById(R.id.btnAktivieren);
    
            btnAktivieren.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    minutenBis = (EditText)findViewById(R.id.etBis);
                    minutenVon = (EditText)findViewById(R.id.etVon);
                    EditText etAktuelleAufgabe = (EditText)findViewById(R.id.etAktuelleAufgabe);
                    if (aktiviert) {
                        btnAktivieren.setText("Aktivieren");
                        aktiviert = false;
                    }
                    else
                    {
                        btnAktivieren.setText("Deaktivieren");
                        Random random = new Random();
                        int w = (int) (random.nextDouble() * (Double.parseDouble(minutenBis.getText().toString()) - Double.parseDouble(minutenVon.getText().toString())) + Double.parseDouble(minutenVon.getText().toString()));
                        int interval = w * 60000;
                        int interval_in_sekunden = w * 60;
                        aktiviert = true;
                        String aufgabe = "";
                        if (etAktuelleAufgabe.getText().toString() == "")
                            aufgabe = "Diese Information";
                        else
                            aufgabe = etAktuelleAufgabe.getText().toString();
    
                        PendingIntent pendingIntent;
                        Intent intent;
    
                        intent = new Intent(con, Kommt.class);
                        intent.putExtra("aufgabe", aufgabe);
                        intent.putExtra("minutenVon", minutenVon.getText().toString());
                        intent.putExtra("minutenBis", minutenBis.getText().toString());
                        pendingIntent = PendingIntent.getBroadcast(con, 0, intent,
                                PendingIntent.FLAG_UPDATE_CURRENT);
    
                        Notification notificationBuilder = new Notification.Builder(con)
                                .setContentTitle(aufgabe)
                                .setContentText("Test")
                                .setSmallIcon(R.mipmap.ic_launcher)
                                .setWhen((System.currentTimeMillis()/1000)+interval_in_sekunden)
                                .setContentIntent(pendingIntent)
                                .setAutoCancel(true)
                                .build();
                        NotificationManager notificationManager = (NotificationManager) getSystemService (Context.NOTIFICATION_SERVICE);
    
                        notificationManager.notify(0, notificationBuilder);
                    }
                }
            });
        }
    }
    

    清单

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="de.test.test">
        <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
        <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
        <application
            android:allowBackup="true"
            android:launchMode="singleTop"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <receiver android:name=".Kommt" />
        </application>
    
    </manifest>
    

    Kommt.java

    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.Toast;
    
    import java.util.Random;
    
    public class Kommt extends BroadcastReceiver {
        public Context con;
        String aufgabe = "";
        String minutenVon = "";
        String minutenBis = "";
        private Context mContext;
        public void onReceive(Context context, Intent intent) {
    
    
            con = context;
            aufgabe = intent.getStringExtra("aufgabe");
            minutenVon = intent.getStringExtra("minutenVon");
            minutenBis = intent.getStringExtra("minutenBis");
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setIcon(R.mipmap.ic_launcher);
            builder.setTitle(aufgabe)
                    .setMessage("Test");
            builder.setPositiveButton("Ja", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    neu();
                }
            });
            builder.setNegativeButton("Nein", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(con);
                    builder.setIcon(R.mipmap.ic_launcher);
                    builder.setTitle("Test")
                            .setMessage("Test2");
                    builder.setPositiveButton("Gut", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            neu();
                        }
                    });
                    builder.create().show();
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    
    
        public void neu()
        {
            Random random = new Random();
            int w = (int) (random.nextDouble() * (Double.parseDouble(minutenBis.toString()) - Double.parseDouble(minutenVon.toString())) + Double.parseDouble(minutenVon.toString()));
            int interval = w * 60000;
            int interval_in_sekunden = w * 60;
            String aufgabe = "";
            if (aufgabe == "")
                aufgabe = "Test";
    
            PendingIntent pendingIntent;
            Intent intent;
    
    
    
            intent = new Intent(con, Kommt.class);
            intent.putExtra("aufgabe", aufgabe);
            pendingIntent = PendingIntent.getBroadcast(con, 0, intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
    
            Notification notificationBuilder = new Notification.Builder(con)
                    .setContentTitle(aufgabe)
                    .setContentText("Testtest3")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setWhen((System.currentTimeMillis()/1000)+interval_in_sekunden)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .build();
            NotificationManager notificationManager = (NotificationManager) con.getSystemService (Context.NOTIFICATION_SERVICE);
    
            notificationManager.notify(0, notificationBuilder);
        }
    
    }
    

4 个答案:

答案 0 :(得分:1)

您正在尝试从BroadcastReceiver打开无法找到上下文的AlertDialog(上下文)。有一件事将用户重定向到broadcastreceiver并从那里显示onCreate中的alertdialog。

问题一:你必须在警告对话框中传递主题

  

AlertDialog.Builder builder = new   AlertDialog.Builder(背景下,   R.style.AppTheme);

其次您无法显示Broadcastreceiver的对话框,因为开发人员希望您知道如何将用户从Broadcastreceiver重定向到活动。 错误你必须为此

崩溃
  

java.lang.RuntimeException:无法启动接收器   com.stackoverflow.Kommit:   android.view.WindowManager $ BadTokenException:无法添加窗口 -   token null不适用于应用程序

因此将用户重定向到MainActivity而不是显示alertDialog,并在MainActivity的onCreate中显示警告对话框,其中您将在MainActivity上有上下文

更新 Kommit.java(onReceive)

Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Bundle b = new Bundle();
b.putInt("notify", 1);
i.putExtras(b); //Put your id to your next Intent 
context.startActivity(i);

在MainActivity中 OnCreate()

Bundle b = getIntent().getExtras();  int value = 0; // or other values
if(b != null) 
value = b.getInt("notify"); 
if(value !=0)
{
 //show alertdialog 
}

P. Dee编辑:更正了您的通知。

答案 1 :(得分:0)

尝试更改不同通知的ID:

notificationManager.notify(0, notificationBuilder);

此处将0更改为某个动态数字

答案 2 :(得分:0)

通常,对话框需要“活动”上下文,或者使对话框键入系统警报对话框

  

alert.getWindow()的setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

这需要在清单中声明android.permission.SYSTEM_ALERT_WINDOW。

答案 3 :(得分:0)

就我而言,onReceive只能存活10秒。因此,如果您的随机时间超过10秒,则会破坏通知对象,该对象将在onReceive中新建。所以我的建议是在Service而不是Broadcast Receiver中的新主题中执行此操作。