android 通知栏按钮,Android通知栏微技巧,通知栏按钮变得不那么敏感

Android通知栏,应始终可见。通知栏有2个按钮,其中第一个是抓一些数据存储和第二个开放活动,将显示所有的数据。我遇到了一个问题,当我关闭应用程序,然后按通知按钮开始活动,活动开始后,我通知按钮停止响应。需要注意的是按键做工精细前点所在的第二按钮点击开始活动。

这里是一个模板代码为我的通知服务和通知栏按钮处理程序

服务处理的通知栏

```

public class NotificationBarService extends Service {

private int notificationID;

@Override

public IBinder onBind(Intent intent){

return null;

}

@Override

public int onStartCommand(Intent intent, int flags, int startId){

notificationID = new Random().nextInt();

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);

contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);

contentView.setTextViewText(R.id.title, "Custom notification");

contentView.setTextViewText(R.id.text, "This is a custom layout");

//Handle the button for showing bookmarks on custom notification

Intent buttonsIntent2 = new Intent(this, NotificationBarButtonActivityHandler.class);

buttonsIntent2.putExtra(PENDING_ACTION, SHOW_BOOKMARKS);

contentView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, PendingIntent.getActivity(this, 0, buttonsIntent2, 0));

//Handle the button for adding bookmark on custom notification

Intent buttonsIntent = new Intent(this, NotificationBarButtonActivityHandler.class);

buttonsIntent.putExtra(PENDING_ACTION, REGISTER_BOOKMARK);

contentView.setOnClickPendingIntent(R.id.notificationBarAddBookmarkFromChromeButton, PendingIntent.getActivity(this, 1, buttonsIntent, 0));

RemoteViews notificationView = new RemoteViews(getPackageName(),

R.layout.custom_notification);

Intent switchIntent = new Intent(this, NotificationBarService.class);

PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,

switchIntent, 0);

notificationView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton,

pendingSwitchIntent);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)

.setContent(contentView)

.setSmallIcon(R.drawable.notification_small_icon)

.setOngoing(true);

Notification notification = mBuilder.build();

startForeground(notificationID, notification);

return START_STICKY;

}

@Override

public void onDestroy(){

super.onDestroy();

stopForeground(true);

}

}

```

类处理按钮按下通知栏

```

public class NotificationBarButtonActivityHandler extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

String action = (String) getIntent().getExtras().get(NotificationBarService.PENDING_ACTION);

if (action != null) {

if (action.equals(NotificationBarService.REGISTER_BOOKMARK)){

CustomLogger.log("---------------- BUTTON FOR COLLECT DATA WAS PRESSED!!!");

//Does something here

}

else if(action.equals(NotificationBarService.SHOW_BOOKMARKS)){

CustomLogger.log("---------------- BUTTON FOR SHOW DATA WAS PRESSSED!!!");

//Notification bar buttons start not responding right after

//this is executed. Note that this problem only occurs if I close the app

//and press the notification button to execute this code.

//Otherwise this works just fine.

Intent intent2;

intent2 = new Intent(this, BookmarkDisplayActivity.class);

startActivity(intent2);

}

}

finish();

}

}

```

最后,我终于解决了这个问题,我在改变,我处理按键的方式。这是我现在的工作。

在NotificationBarService,可以这样处理按钮

```

Intent addBookmarkIntent = new Intent(this, NotificationBarButtonListener.class);

addBookmarkIntent.setAction(ADD_BOOKMARK_ACTION);

PendingIntent pendingAddBookmarkIntent = PendingIntent.getBroadcast(this, 0, addBookmarkIntent, 0);

contentView.setOnClickPendingIntent(R.id.notificationBarAddBookmarkFromChromeButton, pendingAddBookmarkIntent);

Intent showBookmarkIntent = new Intent(this, NotificationBarButtonListener.class);

showBookmarkIntent.setAction(SHOW_BOOKMARK_ACTION);

PendingIntent pendingShowBookmarkIntent = PendingIntent.getBroadcast(this, 0, showBookmarkIntent, 0);

contentView.setOnClickPendingIntent(R.id.notificationBarShowBookmarksButton, pendingShowBookmarkIntent);

```

然后我收到广播甚至和处理这样的

```

public static class NotificationBarButtonListener extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

final String action = intent.getAction();

if(action.equals(ADD_BOOKMARK_ACTION)){

CustomLogger.log("---------------- BUTTON FOR REGISTER BOOKMARK WAS PRESSED!!! ");

}

else if(action.equals(SHOW_BOOKMARK_ACTION)){

CustomLogger.log("---------------- BUTTON FOR SHOW BOOKMARK WAS PRESSSED!!!");

}

### LlamaIndex 在 Python 中的应用场景与实现方法 LlamaIndex 是一种强大的开源框架,用于构建大型语言模型 (LLM) 驱动的应用程序。它通过提供高效的索引机制和灵活的集成能力,在多个领域展现了广泛的应用价值。 #### 1. 核心功能与优势 LlamaIndex 提供了一种简单而高效的方式来管理和查询大量非结构化数据。其主要特点包括支持向量存储、语义检索以及与多种大语言模型的无缝对接[^2]。这些特性使得开发者能够快速搭建复杂的自然语言处理应用。 #### 2. 实现方法详解 以下是利用 LlamaIndex 构建应用程序的一个典型例子: ```python import os from llama_index.core import VectorStoreIndex, SimpleDirectoryReader # 设置 API 密钥环境变量 os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY" # 加载本地文件夹中的文档 documents = SimpleDirectoryReader("data/").load_data() # 创建矢量存储索引 index = VectorStoreIndex.from_documents(documents) # 查询接口设置 query_engine = index.as_query_engine() response = query_engine.query("请解释一下量子力学的基础理论是什么?") print(response) ``` 上述代码展示了如何加载文档并创建一个基于向量存储的索引来执行复杂查询[^1]。 #### 3. 常见问题解决指南 对于初次使用者来说,可能会面临一些基础性障碍,比如未正确配置依赖库或者不熟悉某些特定术语。官方 FAQ 文档提供了详细的解答路径,建议先按照指引完成基本安装与测试工作流[^3]。 #### 4. 结合其他工具扩展功能 除了单独使用外,还可以将 LlamaIndex 和 LangChain 联合起来形成更加强大的解决方案。例如下面这段代码演示了两者协作模式下的对话系统设计思路: ```python from langchain.chains import ConversationalRetrievalChain from llama_index.core import VectorStoreIndex # 初始化 LlamaIndex 向量数据库 vector_store_index = VectorStoreIndex.from_documents(docs) # 定义会话链路对象 conversation_chain = ConversationalRetrievalChain.from_llm( llm=your_preferred_language_model, retriever=vector_store_index.as_retriever(), ) result = conversation_chain({"question": "最近天气怎么样?", "chat_history": []}) print(result['answer']) ``` 此片段说明了当面对多轮交互需求时,可以借助额外组件增强用户体验效果[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值