我试图通过按钮发出通知,这样当点击时我会得到FULL_WAKE_LOCK
,如果我再次按下按钮,FULL_WAKE_LOCK
就会被释放,因为我想要当我在Chrome上阅读某些内容时屏幕不能关闭。
我创建了一个只有一个按钮(在不同布局中)的自定义通知...在主要活动中我使用开关来激活和停用通知...
我尝试在广播接收器类和主类中使用.setText()
....但似乎无法工作......
SO HERE - >我正在尝试使用两种布局noti_switch - >按钮文本ON和noti_switch2 - >按钮文本关闭并根据用户点击交换它们......
//*******THE MAIN ACTIVITY--> contains a switch to spawn Notification with custom button.....******//////
public class Main extends Activity {
public static PowerManager pm;
public static PowerManager.WakeLock wl;
public static NotificationManager nm;
public static Notification noti;
public static Intent intent, intent2, switchIntent1;
public static PendingIntent pending, pending2, switchPending1;
public static RemoteViews rv;
static Context context = null;
public static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = Main.this;
Switch s = (Switch) findViewById(R.id.switch2);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(context, Noti_switch.class);
intent2 = new Intent(context, Noti_switch2.class);
pending = PendingIntent.getActivity(context, 0, intent, 0);
pending2 = PendingIntent.getActivity(context, 0, intent2, 0);
switchIntent1 = new Intent("com.suben.onoffonnotifi.ACTION_ON");
switchPending1 = PendingIntent.getBroadcast(Main.this, 0, switchIntent1, 0);
//switchPending1 = PendingIntent.getService(context, 0, switchIntent1, 0);
s.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
notiTextON();
//I'F I TRY TO GET SPAWN A NEW NOTIFICATION HERE WITH DIFFERENT text ...DOESN'T WORK... IT SEAMS MY PROGRAM IS STUCK IN CLASS extending BROADCAST RECEIVER
} else if (!isChecked) {
if (wl != null)
if (wl.isHeld()) {
wl.release();
}
nm.cancel(0);
}
}
});
}
public void notiTextON() {
Context context = Main.this;
rv = new RemoteViews(getPackageName(), R.layout.noti_switch);
noti = new NotificationCompat.Builder(null)
.setSmallIcon(android.R.drawable.stat_notify_sdcard)
//.setContentTitle("App")
//.setContentText("ON/OFF")
.setContentIntent(pending)
.setContent((RemoteViews) rv).build();
//
noti.contentView = rv;
noti.contentIntent = pending;
noti.flags |= Notification.FLAG_NO_CLEAR;
//
rv.setOnClickPendingIntent(R.id.button1, switchPending1);
nm.notify(0, noti);
}
public void notiTextOFF() {
context = Main.this;
rv = new RemoteViews(getPackageName(), R.layout.noti_switch2);
noti = new NotificationCompat.Builder(null)
.setSmallIcon(android.R.drawable.stat_notify_sdcard)
//.setContentTitle("App")
//.setContentText("ON/OFF")
.setContentIntent(pending2)
.setContent((RemoteViews) rv).build();
//
noti.contentView = rv;
noti.contentIntent = pending2;
noti.flags |= Notification.FLAG_NO_CLEAR;
//
rv.setOnClickPendingIntent(R.id.button2, switchPending1);
nm.notify(0, noti);
}
}
/////***********NOW WORK.JAVA IS THE BROADCASTRECEIVER CLASS*******////
public class Work extends BroadcastReceiver {
public static Main obj;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Main.pm == null) {
Main.pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Main.wl = Main.pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "Arsenal");
}
/*
if(inflater==null){
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.noti_switch, null);
b=(Button) view.findViewById(R.id.button1);
}
*/
if (action.equalsIgnoreCase("com.suben.onoffonnotifi.ACTION_ON")) {
if (!Main.wl.isHeld()) {
Main.wl.acquire();
obj.notiTextOFF(); //I'M TRYING TO CREATE A NEW NOTIFICATION BUT CANT
} else {
if (Main.wl.isHeld()) {
Main.wl.release();
obj.notiTextON(); //I'M TRYING TO SPAWN NEW NOTIFICATION BUT CANT...
}
}
}
}
}