相关疑难解决方法(0)

在Java中,匿名类可以扩展另一个类吗?

代码如:

protected Interface1 varClass1 = new Interface1() {
Run Code Online (Sandbox Code Playgroud)

但我也希望这个匿名的嵌套类也扩展了类Base,如:

protected Interface1 varClass1 = new Interface1() extends Base {
....
Run Code Online (Sandbox Code Playgroud)

这在Java中可行吗?

java nested anonymous class extend

26
推荐指数
1
解决办法
1万
查看次数

在onReceive中取消注册Android广播接收器会抛出"Receiver not registered"

我有BroadcastReceiver一次使用.

我在一个活动中注册它.我不能把它放进去unregisterReceiver(),onPause因为它必须保持运行,即使活动被暂停或销毁.

我希望BroadcastReceiver在完成后取消注册,如下所示:

public class SmsReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
           // do some code..
           context.unregisterReceiver(this)
        }
}
Run Code Online (Sandbox Code Playgroud)

但它会导致异常: Receiver not registered.

android broadcastreceiver

24
推荐指数
2
解决办法
4万
查看次数

BATTERY_PROPERTY_CURRENT_NOW不适用于所有设备。什么是替代品?

我的应用程序使用BatteryManager.BATTERY_PROPERTY_CURRENT_NOW来获取设备的电池电流:

BatteryManager batteryManager = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
int current = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW), context);
Run Code Online (Sandbox Code Playgroud)

但是,它不适用于例如Samsung Galaxy TabA。它仅返回0。

其他应用程序甚至在该设备上显示当前电流。他们是怎么做到的呢?我的方法有哪些替代方法?

java android

12
推荐指数
1
解决办法
315
查看次数

如何在自定义通知文本视图中设置值

请帮助我在自定义通知中的 textview 中设置文本

    private void startNotification()
 {
     String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(ns);
    Notification notification = new Notification(R.drawable.ic_launcher, null, System.currentTimeMillis());
    RemoteViews notificationView = new RemoteViews(getPackageName(),R.layout.mynotification);
    mRemoteViews.setTextViewText(R.id.appName, getResources().getText(0,"gfhf"));
    mBuilder.setContent(mRemoteViews);
    Intent notificationIntent = new Intent(this, FlashLight.class);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.contentView = notificationView;
    notification.contentIntent = pendingNotificationIntent;
    notification.flags |= Notification.FLAG_NO_CLEAR;
    Intent switchIntent = new Intent(this, switchButtonListener.class);
    PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, switchIntent, 0);
    notificationView.setOnClickPendingIntent(R.id.closeOnFlash,  pendingSwitchIntent);
    notificationManager.notify(1, notification);

}
Run Code Online (Sandbox Code Playgroud)

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.RemoteViews.setTextViewText(int, java.lang.CharSequence)”

请帮我解决这个问题。如何在android中的通知中设置textview的值,提前致谢。

sdk android android-notifications

2
推荐指数
1
解决办法
2659
查看次数