我有一个带按钮的自定义通知.要设置通知并使用事件OnClick on my button,我使用了以下代码:
//Notification and intent of the notification
Notification notification = new Notification(R.drawable.stat_notify_missed_call,
"Custom Notification", System.currentTimeMillis());
Intent mainIntent = new Intent(getBaseContext(), NotificationActivity.class);
PendingIntent pendingMainIntent = PendingIntent.getActivity(getBaseContext(),
0, mainIntent , 0);
notification.contentIntent = pendingMainIntent;
//Remoteview and intent for my button
RemoteViews notificationView = new RemoteViews(getBaseContext().getPackageName(),
R.layout.remote_view_layout);
Intent activityIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:190"));
PendingIntent pendingLaunchIntent = PendingIntent.getActivity(getBaseContext(), 0,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationView.setOnClickPendingIntent(R.id.button1,
pendingLaunchIntent);
notification.contentView = notificationView;
notificationManager.notify(CUSTOM_NOTIFICATION_ID, notification);
Run Code Online (Sandbox Code Playgroud)
使用此代码,我使用自定义布局自定义通知...但我无法单击按钮!每次我尝试单击按钮时,我都会单击整个通知,因此脚本会启动"mainIntent"而不是"activityIntent".
我在互联网上看到这段代码不适用于所有终端.我已经在模拟器和HTC Magic上尝试过但我总是遇到同样的问题:我无法点击按钮!
我的代码是对的?有人可以帮帮我吗?
谢谢,
西蒙娜
我有一个带有复选框的活动:如果取消选中chekbox,则停止服务.这是我的活动代码的片段:
Intent serviceIntent = new Intent();
serviceIntent.setAction("com.android.savebattery.SaveBatteryService");
if (*unchecked*){
serviceIntent.putExtra("user_stop", true);
stopService(serviceIntent);
Run Code Online (Sandbox Code Playgroud)
当我停止服务时,我传递一个参数"user_stop"来说明已经是用户停止它的服务,而不是系统(用于低内存).
现在我必须在我的服务的void onDestroy中读取变量"user_stop":
public void onDestroy() {
super.onDestroy();
Intent recievedIntent = getIntent();
boolean userStop= recievedIntent.getBooleanExtra("user_stop");
if (userStop) {
*** notification code ****
Run Code Online (Sandbox Code Playgroud)
但它不起作用!我不能在onDestroy中使用getIntent()!
有什么建议吗?
谢谢
西蒙娜
我有一项服务,在15秒后启动一个线程.这是我的代码片段:
Handler mHandler = new Handler();
OverclockThread ocThread = new OverclockThread();
ocThread.ocPreference = readPreference("oc");
ocThread.serviceOn = true;
if (Intent.ACTION_SCREEN_ON.equals(action)) {
ocThread.screenOff = false;
mHandler.postDelayed(ocThread, 15000);
}
Run Code Online (Sandbox Code Playgroud)
现在我想在这15秒之前添加停止启动ocThread的功能......例如,通过按下按钮或复选框...我必须使用哪个命令来停止使用postDelayed启动的线程?
谢谢
西蒙尼
我已经以NavigationView编程方式添加了项目:
**HERE ADD ITEMS**
Menu rightMenu = mRightDrawerView.getMenu();
for (DataParking dataParking : dataParkingList) {
MenuItem menuItem = rightMenu.add(dataParking.getTimeParking());
}
**HERE ADD CLICK LISTENER**
mRightDrawerView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
// Select menu
menuItem.setChecked(true);
// Closing left_drawer on item click
mDrawerLayout.closeDrawer(mRightDrawerView);
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
导航抽屉中的项目可正确单击,但选择不是持久的。如果我通过XML添加相同的项目,则效果很好。
android ×4
button ×1
menuitem ×1
onclick ×1
ondestroy ×1
parameters ×1
postdelayed ×1
service ×1