显示出警告或视图上接收通知视图、通知

2023-09-05 07:13:13 作者:盖世可爱

我在下面的 这 教程的 Android设备上显示的的通知的。当我跑在设备上的应用程序,图标出现在状态栏(因为它通常出现在Android设备),这是绝对完美的。但只是出于好奇,我想知道,我可以显示一个警告或当设备收到通知一些细节的一些看法?我想实现这个概念在我的下一个应用。 一些示例将极大地帮助我。

I was following this tutorial for displaying a notification on an Android device. When I ran the application on the device, an icon appeared on the status bar (as usually it appears on Android device) which is absolutely perfect. But just out of curiosity I wanted to know that can I display an alert or some view with few details when device receives a notification? I want to implement this concept in my next application. Some sample would greatly help me.

推荐答案

一个典型的一种模式会为你注册你的应用程序的某一部分,以接收,或聆听,具体意图。这样一来,你的应用程序可以在任意时间点醒来,看调用的意图,并决定如何处理它(无论是开头的完整应用程序,显示一个对话框,或者别的什么)。一个很好的事情,伴随着这是使用AlarmManager设置一个报警,将火灾和唤醒您在未来的应用程序(定期更新)。

A typical kind of pattern would be for you to register a certain part of your application to "receive", or listen for, specific intents. This way, your application can wake up at any arbitrary point in time, look at the calling intent, and decide how to handle it (be it starting the full application, displaying a dialog, or something else). One good thing that goes along with this is to use the AlarmManager to set an alarm that will fire and wake up your application in the future (periodic updates).

这是否回答你的问题?我做了这样的事情在我的应用程序,我可以帮你用code,如果你喜欢。

Does this answer your question? I've done something like this in my application and I can help you with the code if you like.

修改来实现这一点,你会做一个扩展或者BroadcastReceiver的或IntentService(取决于如果您希望类分别在UI线程或作为服务运行)的Java类。在我下面的例子中,我定义我自己的意图操作键,但通常你会检查与intent.getAction()的作用:

EDIT to implement this you would make a java class that extends either BroadcastReceiver or IntentService (depending on if you want the class to run on the ui thread or as a service respectively). In my example below, I have defined my own intent action key, but typically you would check the action with intent.getAction():

public class QueryService extends IntentService {
public final static String SERVICE_NAME = "QueryService";

// incoming flags
public final static String FLAG_ACTION = "R_ACTION";
public final static String FLAG_EVENTS_RETURNED = "R_EVENTS";
public final static String FLAG_EVENTS_ARCHIVED_RETURNED = "R_EVENTS_ARCH";
public final static String FLAG_SHARED_PREFERNCES_RETURNED = "R_PREFS";

// outgoing flags
public final static String RETURN_EVENTS = "RETURN_E";
public final static String RETURN_EVENTS_ARCHIVED = "RETURN_E_A";
public final static String RETURN_SHARED_PREFS = "RETURN_S_P";

public QueryService() {
    super(SERVICE_NAME);
}

protected void onHandleIntent(Intent intent) {
    String rAction = intent.getStringExtra(FLAG_ACTION);
    boolean rEvents = intent.getBooleanExtra(FLAG_EVENTS_RETURNED, true);
    boolean rEventsArchived = intent.getBooleanExtra(FLAG_EVENTS_ARCHIVED_RETURNED, true);
    boolean rSharedPrefs = intent.getBooleanExtra(FLAG_SHARED_PREFERNCES_RETURNED, true);

    if(rAction == null){
        Log.e(SERVICE_NAME, "no return action specified, exiting...");
        return;
    }

    Log.i(SERVICE_NAME, "Caller: " + rAction);

    DroidTaskApplication app = (DroidTaskApplication)getApplicationContext();

    Intent resultsIntent = new Intent(rAction);
    // TODO assembling events / archived events from a database needs
    // a function that gets the complete event instead of just its headers

    // assemble event objects and insert them
    if(rEvents){
        List<Event> liteEvents = app.edo.getAllEvents();
        if(liteEvents != null){
            for(Event e : liteEvents){
                int id = e.getId();
                e.setAlarms(app.edo.getAlarmsById(id));
                e.setSubtasks(app.edo.getSubtasksById(id));
                e.setNotes(app.edo.getNotesById(id));
            }
        }
        resultsIntent.putExtra(RETURN_EVENTS, (Serializable)liteEvents);
    }
    // assemble archived event objects and insert
    if(rEventsArchived){
        List<Event> liteEventsA = app.edo.getAllArchivedEvents();
        resultsIntent.putExtra(RETURN_EVENTS_ARCHIVED, (Serializable)liteEventsA);
    }
    // collect the shared data and send it
    if(rSharedPrefs){
        SharedPreferences prefs = getSharedPreferences(getString(R.string.PREFS_FILE_NAME), MODE_WORLD_READABLE);
        resultsIntent.putExtra(RETURN_SHARED_PREFS, (Serializable)(prefs == null? null : prefs.getAll()));
    }           

    Log.i(SERVICE_NAME, "returning results");

    // send everything to the caller
    sendBroadcast(resultsIntent);
 }

}

类无论是否是一个服务或广播接收机,必须使用该应用程序作为接收器进行注册(这意味着它可以接受的某种意图广播);这可以在Java code或Android清单来完成。我使用清单中的服务下面是一个例子(尽管我的Java类不关心,对意图的动作的但的):

    <!-- Notification Launcher -->
    <receiver android:name="edu.clarkson.dtask.BK.NotificationReceiver" android:enabled="true">
        <intent-filter>
            <action android:name="edu.clarkson.dtask.BK.NotificationReceiver.DISPATCH_ALARM" />
            <action android:name="edu.clarkson.dtask.BK.NotificationReceiver.CANCEL_ALARM" />
        </intent-filter>
    </receiver>

不同的系统操作和通知将在一定状态变化的操作系统中播放;如果你想听到这些变化,你注册一个BroadcastReceiver或intentservice如上述清单中的相同的方式为不同的意图的动作(如ACTION_BATTERY_STATE_CHANGED)。所有的文档可以在 Android开发网站找到。我希望这将帮助您开始在正确的道路上。

Different system actions and notifications will be broadcast by the OS when a certain state changes; if you would like to hear about these changes, you register a broadcastreceiver or intentservice the same way as above in the manifest for a different Intent action (such as ACTION_BATTERY_STATE_CHANGED). All the documentation can be found on the android dev site. I hope that will get you started on the right path.

 
精彩推荐
图片推荐