Kак вызвать метод из .onReceive?
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// Идентификатор уведомления
private static final int NOTIFY_ID = 1010;
// Идентификатор канала
private static String CHANNEL_ID = "quote_chanel";
ArrayHold arr = new ArrayHold();
GenNumber genNumber = new GenNumber();
private TextView mTextView;
private String bCopy;
private String bNext;
private String bClose;
private String appName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.textView);
bCopy = getString(R.string.copy);
bNext = getString(R.string.next);
bClose = getString(R.string.close);
appName = getString(R.string.app_name);
createNotificationChannel();
printText();
}
private void printText() {
genNumber.genRanNumb();
int i = genNumber.mRandom;
mTextView.setText(arr.quote[i]);
Intent nextIntent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,
0, nextIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent copyIntent = new Intent(MainActivity.this, BroadcastCopyReceiver.class);
PendingIntent copendingIntent = PendingIntent.getBroadcast(MainActivity.this,
0, copyIntent, 0);
Intent cancelIntent = new Intent(MainActivity.this, NotificationCancelReceiver.class);
cancelIntent.putExtra("notificationId",NOTIFY_ID);
PendingIntent cPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, cancelIntent, 0);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_format_quote_black_24dp)
.setContentTitle(appName + " (" + i + "/" + arr.quote.length + ")")
.setContentText(arr.quote[i])
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.addAction(R.drawable.ic_content_copy_black_24dp, bCopy, copendingIntent)
.addAction(R.drawable.ic_arrow_forward_black_24dp, bNext, pendingIntent)
.addAction(R.drawable.ic_cancel_black_24dp, bClose, cPendingIntent);
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(MainActivity.this);
notificationManager.notify(NOTIFY_ID, builder.build());
}
private void createNotificationChannel() {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BroadcastCopyReceiver extends BroadcastReceiver {
ClipboardManager clipboardManager;
ClipData clipData;
ArrayHold arr = new ArrayHold();
GenNumber genNumber = new GenNumber();
public BroadcastCopyReceiver () {
}
@Override
public void onReceive(Context context, Intent intent) {
int i = genNumber.mRandom;
String arrayExtra = arr.quote[i];
clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipData = ClipData.newPlainText("copyarr", arrayExtra);
clipboardManager.setPrimaryClip(clipData);
Toast.makeText(context.getApplicationContext(),arrayExtra,Toast.LENGTH_SHORT).show();
}
}
я бы хотел что бы при нажатии на кнопку из строки уведомления запускался код из BroadcastCopyReceiver.
Который копировал текст из массива
public class ArrayHold {
String[] quote
Hасколько я полагаю нужно создать еще один класс который будет выполнять копирование и вызывать его из BroadcastCopyReceiver ?