如何使用BrodcastReceiver

时间:2019-04-22 12:36:51

标签: broadcastreceiver

我不明白如何传递Contex,或者更确切地说需要传递什么。我在互联网上找到了该通知的代码段,现在,我正在这样做,以便通过特定时间每天到达通知,但是我无法转移BroadcastReceiver。如何实现?

package com.example.testpush;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Color;

public class NotificationUtils extends ContextWrapper {

    private NotificationManager mManager;
    public static final String ANDROID_CHANNEL_ID = "com.chikeandroid.tutsplustalerts.ANDROID";
    public static final String ANDROID_CHANNEL_NAME = "ANDROID CHANNEL";

    public NotificationUtils(Context base) {
        super(base);
        createChannels();
    }

    public void createChannels() {
        // create android channel
        NotificationChannel androidChannel = new NotificationChannel(ANDROID_CHANNEL_ID,
                ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        // Sets whether notifications posted to this channel should display notification lights
        androidChannel.enableLights(true);
        // Sets whether notification posted to this channel should vibrate.
        androidChannel.enableVibration(true);
        // Sets the notification light color for notifications posted to this channel
        androidChannel.setLightColor(Color.GREEN);
        // Sets whether notifications posted to this channel appear on the lockscreen or not
        androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        getManager().createNotificationChannel(androidChannel);
    }

    NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }
    public Notification.Builder getAndroidChannelNotification(String title, String body) {
        return new Notification.Builder(getApplicationContext(), ANDROID_CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(android.R.drawable.ic_popup_disk_full)
                .setAutoCancel(true);
    }

}



package com.example.testpush;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.widget.EditText;

public class test extends BroadcastReceiver {

    private NotificationUtils mNotificationUtils;
    private MainActivity main;

    @Override
    public void onReceive(Context context, Intent intent) {
        main=new MainActivity();
       mNotificationUtils=new NotificationUtils();//How pass contex ??

       // final EditText editTextTitleAndroid = (EditText) main.findViewById(R.id.et_android_title);
        //final EditText editTextAuthorAndroid = (EditText) main.findViewById(R.id.et_android_author);
        String title = "Test title";
        String author = "Test text some";

        if(!TextUtils.isEmpty(title) && !TextUtils.isEmpty(author)) {
            Notification.Builder nb = mNotificationUtils.
                    getAndroidChannelNotification(title, "By " + author);

            mNotificationUtils.getManager().notify(101, nb.build());        }

    }
}


package com.example.testpush;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {

    public NotificationUtils mNotificationUtils;

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

        mNotificationUtils = new NotificationUtils(this);
        Button buttonAndroid = (Button) findViewById(R.id.btn_send_android);

        buttonAndroid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startAlarm();
            }
        });
    }
    private void startAlarm() {
        AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent myIntent;
        PendingIntent pendingIntent;
        myIntent = new Intent(MainActivity.this,ttt.class);
        //pendingIntent = PendingIntent.getService(this,0,myIntent,0);
         //   manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+3000,pendingIntent);
        pendingIntent=PendingIntent.getActivity(this,0,myIntent,0);
        manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime()+30000,pendingIntent);
    }
 }

这是正在进行初始化的行中的错误代码。

mNotificationUtils=new NotificationUtils();

0 个答案:

没有答案