制作一个通知图标出现在开机/耳机插件 [英] Making a notification icon appear on boot/headset plugin

查看:97
本文介绍了制作一个通知图标出现在开机/耳机插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使它所以当一个插在那里耳机会显示一个通知图标。我做了这么当手机打开这个运行它启动它具有code在onCreate方法的通知图标因此它只是自动启动MainActivity类。这问题是,它启动了整个活动,并应用程序,这是我不想要的。我只是想显示的图标。我怎么能去呢?谢谢!

 公共无效的onReceive(上下文的背景下,意图意图){
    // TODO自动生成方法存根
    意图myIntent =新的意图(背景下,MainActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);
}
 

以上code开头的MainActivity上启动。

通知图标code

  //通知图标启动
    NotificationManager纳米=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    通知通知=新的通知(R.drawable.icon_notification图标的通知,System.currentTimeMillis的());
    上下文的背景下= MainActivity.this;
    PendingIntent contentIntent = PendingIntent.getActivity(此,0,新的意向(这一点,MainActivity.class),Notification.FLAG_ONGOING_EVENT);
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(这一点,通知图标,触更多的选择,contentIntent);
    意向意图=新的意图(背景下,MainActivity.class);
    PendingIntent未决= PendingIntent.getActivity(上下文,0,意图,0);
    nm.notify(0,通知);
    //通知图标完
 

解决方案

我贴一个解决方案,让您监视耳机状态(有线和蓝牙),<一个href="http://stackoverflow.com/questions/16409309/how-to-detect-headphone-plug-event-in-offline-mode/19102661">over这里。该解决方案可以很容易地扩展到允许你显示一个通知。

I want to make it so when one plugs in there headset a notification icon appears. I've made it so when the phone turns on this runs which starts the MainActivity class which has the code for the notification icon in the OnCreate method so it just automatically starts. The problem with that is that it starts the whole activity and app, which I don't want. I just want the icon to appear. How could I go about this? Thank You!

public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent(context, MainActivity.class);
    myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(myIntent);
}

The above code starts the MainActivity on boot.

Notification Icon Code

    //Notification Icon Starts
    NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification=new Notification(R.drawable.icon_notification, "Icon Notification", System.currentTimeMillis());
    Context context=MainActivity.this;
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), Notification.FLAG_ONGOING_EVENT);        
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(this, "Notification Icon", "Touch for more options", contentIntent);
    Intent intent=new Intent(context,MainActivity.class);
    PendingIntent  pending=PendingIntent.getActivity(context, 0, intent, 0);
    nm.notify(0, notification);
    //Notification Icon Ends

解决方案

I posted a solution that allows you to monitor the headphone state (wired and bluetooth), over here. This solution could easily be extended to allow you to display a notification.

这篇关于制作一个通知图标出现在开机/耳机插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆