Android 安卓通知onclick
我想知道如何为android通知实现Android 安卓通知onclick,android,android-intent,notifications,android-pendingintent,Android,Android Intent,Notifications,Android Pendingintent,我想知道如何为android通知实现onClickListener。我试图在通知中实现sendText(),而不是将用户发送到主活动: public class AlertReceiver extends BroadcastReceiver { Context mContext; String number; String messageList; String name; @Override public void onReceive(Conte
onClickListener
。我试图在通知中实现sendText()
,而不是将用户发送到主活动:
public class AlertReceiver extends BroadcastReceiver {
Context mContext;
String number;
String messageList;
String name;
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
name = intent.getStringExtra("name");
messageList = intent.getStringExtra("messageList");
number = intent.getStringExtra("number");
createNotification(context, "times up " + name, "5 seconds passed!", "alert");
}
public void createNotification(Context context, String message, String messageText, String messageAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(message)
.setTicker(messageText)
.setContentText(messageAlert);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
public void sendText(){
//Turn string of all messages into an ArrayList in order to get one specific message at random
ArrayList<String> messagesArrayList = null;
try {
messagesArrayList = Utility.getArrayListFromJSONString(messageList);
} catch (JSONException e) {
e.printStackTrace();
}
Random rand = new Random();
//todo the following may cause a bug if there are no messages in list
int n = rand.nextInt(messagesArrayList.size());
String message = messagesArrayList.get(n);
try {
//send text message
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(number, null, message, null, null);
Toast.makeText(mContext, "Message Sent",
Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
//If text message wasn't sent attempt to send text another way (through the user's text messaging app)
// Most likely due to text message permissions not being accepted by user
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + number)); // This ensures only SMS apps respond
intent.putExtra("sms_body", message);
if (intent.resolveActivity(mContext.getPackageManager()) != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}
}
}
}
公共类AlertReceiver扩展了BroadcastReceiver{
语境;
字符串编号;
字符串消息列表;
字符串名;
@凌驾
公共void onReceive(上下文、意图){
mContext=上下文;
name=intent.getStringExtra(“名称”);
messageList=intent.getStringExtra(“messageList”);
编号=intent.getStringExtra(“编号”);
createNotification(上下文,“times up”+名称,“5秒过去!”,“警报”);
}
public void createNotification(上下文上下文、字符串消息、字符串消息文本、字符串消息警报){
PendingEvent notificIntent=PendingEvent.getActivity(上下文,0,新意图(上下文,MainActivity.class),0);
NotificationCompat.Builder mBuilder=新建NotificationCompat.Builder(上下文).setSmallIcon(R.mipmap.ic_启动器)
.setContentTitle(消息)
.setTicker(messageText)
.setContentText(messageAlert);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT\u声音);
mBuilder.setAutoCancel(真);
NotificationManager mNotificationManager=(NotificationManager)context.getSystemService(context.NOTIFICATION\u服务);
mNotificationManager.notify(1,mBuilder.build());
}
public void sendText(){
//将所有消息的字符串转换为ArrayList,以便随机获取一条特定消息
ArrayList messagesArrayList=null;
试一试{
messagesArrayList=Utility.getArrayListFromJSONString(messageList);
}捕获(JSONException e){
e、 printStackTrace();
}
Random rand=新的Random();
//如果列表中没有消息,执行以下操作可能会导致错误
int n=rand.nextInt(messagesArrayList.size());
String message=messagesArrayList.get(n);
试一试{
//发送短信
SmsManager SmsManager=smsmsmanager.getDefault();
smsManager.sendTextMessage(number,null,message,null,null);
Toast.makeText(mContext,“消息已发送”,
吐司。长度(短)。show();
}捕获(例外情况除外){
//如果未发送短信,请尝试以其他方式发送短信(通过用户的短信应用程序)
//很可能是由于用户不接受短信权限
意向意向=新意向(意向.行动\发送到);
intent.setData(Uri.parse(“smsto:+number”);//这确保只有SMS应用程序响应
intent.putExtra(“短信正文”,信息);
if(intent.resolveActivity(mContext.getPackageManager())!=null){
intent.addFlags(intent.FLAG\u活动\u新任务);
mContext.startActivity(意图);
}
}
}
}
注意:以下信息并非真正必要。这主要是因为stackoverflow认为我的代码与文本的比率太低,但也可能有助于澄清一点:
sendText()
基本上是一种在不打开新活动的情况下尝试发送预制文本消息的方法。但是,如果没有权限,它将使用意图打开新活动。因此,为了尽量减少出现的屏幕数量并使用户使用起来更简单,我尝试使用sendtext
方法进行操作。如果您不想将用户发送到某个活动,则可以启动一个服务:pendingent.getService(…)
当用户单击通知并执行将文本发送到那里的任务时。如果您不想将用户发送到某个活动,则可以在用户单击通知并执行将文本发送到那里的任务时启动服务:PendingEvent.getService(…)
。尝试以下操作:
public class AlarmReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
@Override
public void onReceive(Context context, Intent intent) {
// here DoSomething is your service name that you want to start
Intent myIntent = new Intent(context, DoSomething.class);
PendingIntent pendingIntent = PendingIntent.getService(
context,
0,
myIntent,
0);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("Do Something...")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
试试这个:
public class AlarmReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
@Override
public void onReceive(Context context, Intent intent) {
// here DoSomething is your service name that you want to start
Intent myIntent = new Intent(context, DoSomething.class);
PendingIntent pendingIntent = PendingIntent.getService(
context,
0,
myIntent,
0);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("Do Something...")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
您可以创建PendingEvent来启动广播接收器,而不是创建PendingEvent来启动活动,如下所示
PendingIntent intent = PendingIntent.getBroadcast(context, 0, new Intent(context, SendTextReceiver.class), 0);
因此,当您单击通知时,它将调用您的广播接收器SendTextReceiver并在其中执行sendText逻辑,这样您就不必总是启动活动,并且您的逻辑将在没有活动的情况下完成,而不是创建挂起内容来启动活动,您可以创建一个悬挂式帐篷来启动广播接收器,如下所示
PendingIntent intent = PendingIntent.getBroadcast(context, 0, new Intent(context, SendTextReceiver.class), 0);
因此,当您单击通知时,它将调用您的广播接收器SendTextReceiver并在其中执行sendText逻辑,这样您就不必总是启动活动,并且您的逻辑将在没有活动的情况下完成