简体   繁体   English

屏幕打开时更新Android窗口小部件

[英]Update an Android Widget when the screen is turned ON

The question is simple as it seems. 这个问题看起来很简单。

The Android Widget works as a charm, everything is OK. Android Widget很有魅力,一切正常。

I just want the widget content to be updated when (in the right moment) the user turn on its device screen . 我只希望在(在适当的时候) 用户打开其设备屏幕 更新窗口小部件内容。

I can't find a hint for this in internet nor the documentation. 我在互联网和文档中都找不到关于此的提示。 This means that I'm clearly overlooking something easy and important. 这意味着我显然忽略了一些简单而重要的事情。

UPDATE 更新

Thanks for the answer of Murtaza, seems perfect, but for some reason isn't working. 感谢Murtaza的回答,似乎很完美,但是由于某些原因无法正常工作。 My widget has already a receiver, so i added the suggested intent filter: 我的小部件已经有一个接收器,所以我添加了建议的意图过滤器:

    <receiver android:name=".MyWidget" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.SCREEN_OFF" />
        </intent-filter>

Inside the widget class i overriden the suggested function: 在小部件类中,我重写了建议的功能:

@Override
  public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
      playNotification(context);
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
      playNotification(context);
    }
 }

The playNotification() plays an alarm. playNotification()会发出警报。 It works properly inside the widget. 它在小部件内正常工作。 But when I switch the screen ON and OFF, nothing happens. 但是当我打开和关闭屏幕时,什么也没发生。

UPDATE II - A working widget in Android UPDATE II-Android中的工作小部件

I post the whole code of the widget for who need it. 我为需要它的人张贴了小部件的全部代码。

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.widget.RemoteViews;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Calendar;


/**
 * Implementation of App Widget functionality.
 */
public class MyWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    // There may be multiple widgets active, so update all of them
    final int N = appWidgetIds.length;
    for (int i = 0; i < N; i++) {
        updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        playNotification(context, true);
    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        playNotification(context, true);
    }
}

static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                            int appWidgetId) {
    String widgetText = "";

    playNotification(context, true);

    widgetText += " DATE:" + Calendar.getInstance().getTime().getHours() + ":" + Calendar.getInstance().getTime().getMinutes();
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.mywidget);
    views.setTextViewText(R.id.appwidget_text, widgetText);
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

public static void playNotification(Context context, boolean alarm) {

    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        if (alarm) notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        if (!r.isPlaying())
            r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

ANDROID MANIFEST 机器人清单

This is what you need to add to the manifest to have a working widget on Android. 这是您需要添加到清单中以便在Android上具有有效的小部件的功能。 More, the filters I would like to make work. 另外,我想制作过滤器。

   <receiver android:name=".MYWidget" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.SCREEN_OFF" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/mywidget_info" />
    </receiver>

PLEASE NOTE THAT the code works. 请注意该代码有效。 Everything except what concern my question: "Update an Android Widget when the screen is turned ON" 除了我所关心的问题外,其他所有内容:“屏幕打开时更新Android窗口小部件”

You need to create a Broadcast Receiver for that. 您需要为此创建一个广播接收器。

Add these permissions in your Manifest.xml 在您的Manifest.xml中添加这些权限

 <receiver android:name=".MyBroadcastReceiver" android:enabled="true">
    <intent-filter>
       <action android:name="android.intent.action.ACTION_SCREEN_ON"></action>
       <action android:name="android.intent.action.ACTION_SCREEN_OFF"></action>
    </intent-filter>
</receiver>

MyBroadcastReceiver.java MyBroadcastReceiver.java

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
           //update your widget.
        }
    }

}

The question turns into a new one, so I want to show some example. 这个问题变成一个新问题,因此我想举一些例子。 Your question now is, how to update the widget from inside all other classes. 您现在的问题是,如何从所有其他类中更新窗口小部件。 As I suspect, Your playNotification(); 我怀疑您的playNotification(); method is inside the receiver of Your widget. 方法在您的小部件的接收器内部。 What You have to do is, make an extra class with Your playNotification() method as public , so You can call it from every other class. 您需要做的是,使用您的playNotification()方法将一个额外的类设置为public,以便您可以从其他每个类中调用它。 Look this example, based on the answer from Murtaza and Your update: 根据Murtaza和“您的更新”的答案看这个例子:

create an extra class: 创建一个额外的类:

 public class NotificationHelper{
 private Context mContext;


 //make a constructor and set Context or other objects/values if You need it
  public NotificationHelper(Context ctx){

     mContext = ctx;
  }


  public void playNotification(){

    //do Your stuff here inside
  }

}

With this public class, You can do Your stuff from anywhere else. 通过此公开课,您可以在其他任何地方做自己的事情。 For example inside Your AppWidget Receiver or Your BroadCastReceiver that will be fired if the screen goes on or off: 例如,在您的AppWidget Receiver或BroadCastReceiver内部,如果屏幕打开或关闭,将被触发:

   @Override
   public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
     NotificationHelper mHelper = new NotificationHelper(context);
     mHelper.playNotification();
   } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
     NotificationHelper mHelper = new NotificationHelper(context);
     mHelper.playNotification();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 屏幕打开/关闭时如何更新 Android 小部件? - How to update an Android widget when the screen is turned On/Off? 如果屏幕关闭,如何更新时钟小部件? - How to update clock widget if screen is turned off? 屏幕关闭时,Android加速度计无法正常工作 - Android accelerometer not working when screen is turned off 关闭屏幕后如何停止小部件android的更新? - how to stop the update of the widget android when I turn off the screen? Android-屏幕关闭或屏幕超时时终止应用 - Android - Kill an app when screen turned off or screen times out 如何在Android 5.0 Lollipop中关闭屏幕固定时收到通知? - How to be notified when screen pinning is turned off in Android 5.0 Lollipop? 屏幕关闭时,服务中的android加速计停止运行 - android accelerometer in service stops when screen is turned off 在Android中关闭屏幕时如何防止CPU“休眠”? - How to keep CPU from 'sleeping' when screen is turned off in Android? 屏幕关闭时调用Android onStart()事件 - Android onStart() event is called when the screen is turned off 是否可以在 JavaScript 中检测到 Android 和 iOS 浏览器中的屏幕何时关闭 - Is it possible, in JavaScript, to detect when the screen is turned off in the Android & iOS browsers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM