所以,我有一个main活动,它有一个启动服务的按钮,MyService。我希望该服务显示带有自定义标题、文本、图标等的通知。该服务是一个前台服务。不是绑定服务。一切正常,但唯一的一点是关于我的通知是不断显示“点击获取更多信息”,点击它会引导我进入应用程序的设置页面。尽管我的主要活动是悬而未决的。
我想要的是有一个带有自定义标题、文本、图标和停止服务的操作的通知。
这是我的主要活动。java类
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start_tracking).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startCommand();
}
});
}
void startCommand() {
startService(new Intent(this, MyService.class));
}
}
我的服务。java文件
public class MyService extends Service {
boolean mServiceIsStarted = false;
void moveToStartedState() {
Intent myIntentbuilder = new IntentBuilder(this).setmCommandId(Command.START).build();
Log.d(TAG, "moveToStartedState: Running on Android O - startForegroundService(intent)");
ContextCompat.startForegroundService(this, myIntentbuilder);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: ");
boolean containsCommand = IntentBuilder.containsCommand(intent);
routeIntentToCommand(intent);
return START_NOT_STICKY;
}
void routeIntentToCommand(Intent intent) {
if (intent != null) {
if (IntentBuilder.containsCommand(intent)) {
processCommand(IntentBuilder.getCommand(intent));
} else
commandStart();
}
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
}
void showNotification() {
Log.d(TAG, "showNotification: ");
HandleNotification.O.createNotification(this);
}
private void processCommand(int command) {
try {
switch (command) {
case Command.START:
commandStart();
break;
case Command.STOP:
commandStop();
break;
}
} catch (Exception e) {
e(TAG, "processCommand: exception", e);
}
}
void commandStop() {
stopSelf();
stopForeground(true);
}
void commandStart() {
if (!mServiceIsStarted) {
mServiceIsStarted = true;
moveToStartedState();
return;
}
//
HandleNotification.O.createNotification(this);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
IntentBuilder。java类
@IntDef({Command.INVALID, Command.STOP, Command.START})
@Retention(RetentionPolicy.SOURCE)
@interface Command {
int INVALID = -1;
int STOP = 0;
int START = 1;
}
public class IntentBuilder {
private static final String KEY_MESSAGE = "msg";
private static final String KEY_COMMAND = "cmd";
private Context mContext;
private String mMessage;
private @Command
int mCommandId = Command.INVALID;
public static IntentBuilder getInstance(Context mContext) {
return new IntentBuilder(mContext);
}
public IntentBuilder(Context mContext) {
this.mContext = mContext;
}
public void setmContext(Context mContext) {
this.mContext = mContext;
}
public IntentBuilder setmMessage(String mMessage) {
this.mMessage = mMessage;
return this;
}
public IntentBuilder setmCommandId(int mCommandId) {
this.mCommandId = mCommandId;
return this;
}
private static final String TAG = "IntentBuilder";
public Intent build() {
Log.e(TAG, "build: context cannot be null" + mContext);
Intent intent = new Intent(mContext, MyService.class);
if (mMessage != null)
intent.putExtra(KEY_MESSAGE, mMessage);
if (mCommandId != Command.INVALID)
intent.putExtra(KEY_COMMAND, mCommandId);
return intent;
}
public static boolean containsCommand(Intent intent) {
return intent.getExtras() != null && intent.getExtras().containsKey(KEY_COMMAND);
}
public static boolean containsMessage(Intent intent) {
return intent.getExtras() != null && intent.getExtras().containsKey(KEY_MESSAGE);
}
public static @Command
int getCommand(Intent intent) {
final @Command int commandId = intent.getExtras().getInt(KEY_COMMAND);
return commandId;
}
public static String getMessage(Intent intent) {
return intent.getExtras().getString(KEY_MESSAGE);
}
}
手语提示。java文件
public class HandleNotification {
public static class O {
public static int getRandomNumber() {
return new Random().nextInt(100000);
}
static PendingIntent getLaunchActivityPI(Service context) {
Intent intent = new Intent(context, MainActivity.class);
return PendingIntent.getActivity(context, getRandomNumber(), intent, 0);
}
static PendingIntent getStopServicePI(Service context) {
PendingIntent pendingIntent;
{
Intent intent = new IntentBuilder(context).setmCommandId(Command.STOP).build();
pendingIntent = PendingIntent.getService(context, getRandomNumber(), intent, 0);
}
return pendingIntent;
}
public static final Integer ONGOING_NOTIFICATION_ID = getRandomNumber();
public static final String CHANNEL_ID = String.valueOf(getRandomNumber());
private static final String TAG = "O";
public static void createNotification(Service context) {
Log.d(TAG, "createNotification: ");
String channelId = createChannel(context);
Notification notification = buildNotification(channelId, context);
context.startForeground(ONGOING_NOTIFICATION_ID, notification);
}
static Notification buildNotification(String channelId, Service context) {
PendingIntent piMainActivity = getLaunchActivityPI(context);
PendingIntent piStopService = getStopServicePI(context);
Icon poweroff = Icon.createWithResource(context, android.R.drawable.star_big_on);
Notification.Action stopAction = new Notification.Action
.Builder(poweroff, "STOP", piStopService).build();
return new Notification.Builder(context, channelId)
.addAction(stopAction)
.setContentTitle("Tracking...")
.setContentText("I'm tracking your location now... ")
.setContentIntent(piMainActivity)
.build();
}
@NonNull
private static String createChannel(Context service) {
NotificationManager notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence channelName = "Start Location Updates";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, channelName, importance);
notificationManager.createNotificationChannel(notificationChannel);
return CHANNEL_ID;
}
}
}
使用NotificationCompat。生成器
。你可以在这里学习如何创建一个
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(textTitle)
.setContentText(textContent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
您可以添加自定义的大小图标、标题、内容和其他属性。
我很久以前做过的一个项目的例子
private void LockNotification() {
NotificationCompat.Builder builder = new
NotificationCompat.Builder(getApplicationContext());
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("key","launch_about");
PendingIntent pendingIntent =
PendingIntent.getActivity(getApplicationContext(), 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Set the title, text, and icon
builder.setContentTitle(getString(R.string.app_name))
.setContentText("App Lock Enabled")
.setSmallIcon(R.drawable.ic_applock)
.setContentIntent(pendingIntent)
.setOngoing(true);
// Get an instance of the Notification Manager
NotificationManager notifyManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// Build the notification and post it
notifyManager.notify(0, builder.build());
}
奇怪的是,自定义图标在三星Mini(jelly bean)和Nexus 4(API 27)模拟器上显示正确,但在三星A6(Oreo)和华为Honory Lite 9(Oreo)设备上却显示不正确,在这些设备上,我看到了默认的绿色背景白色droid图标。 通知始终显示,但在某些设备上不使用自定义图标。 我尝试在AssetStudio中实现它,并且使用和不使用Notification Builder实
所以我正在使用asmack库来侦听传入的xmpp数据包。我已经实现了服务。它启动,绑定,运行一个方法,然后解除绑定。该服务创建PacketListener以获取传入消息,并在新消息传入时显示通知。当应用程序处于活动状态时,这一切都起作用,但当它完全关闭(在窗口列表中滑动)时就不起作用了,它只是在运行服务,PacketListener不启动。但只要重新打开应用程序,它就会立即启动,关闭所有通知。我正
说明: 有谁遇到过这样的问题?我需要任何提示来解决它,因为它看起来很奇怪,因为我有所有4个键入的.png图标在mipmap文件夹。
本文向大家介绍Android 设置自定义通知-显示全文,包括了Android 设置自定义通知-显示全文的使用技巧和注意事项,需要的朋友参考一下 示例 如果要在上下文中显示长文本,则需要设置自定义内容。 例如,您有: 但您希望您的文字能完整显示: 您需要做的就是向您的内容添加样式,如下所示:
问题内容: 在创建我自己的SimpleAdapter对象之前,因为我想更改行的颜色,所以我只是使用new SimpleAdapter(…)。现在,我正在使用自己的自定义SimpleAdapter,行颜色正在更改,但是我的文本没有得到更新。我已经调用了adapter.notifyDataSetChanged(),但它仍只显示示例文本“ TextView”。正如我所说,当我没有创建自己的适配器时,一切
我们正在尝试将DataDog与我们的Ruby On Rails应用程序集成。我们的ROR应用程序将每秒不断添加用户、更新用户和删除用户。 我已经集成了Datadog来监控数量。通过Datadog提供的图表添加、更新和删除的用户。 我使用Ubuntu Aws实例的命令安装了datadog代理。 我得到了14天的免费试用。 我遵循dogstatd ruby gem的以下文档:https://githu