在收到通知时显示警报或视图

6 notifications android

我正在按照教程在Android设备上显示通知.当我在设备上运行应用程序时,状态栏上会出现一个图标(通常它出现在Android设备上),这绝对是完美的.但出于好奇,我想知道当设备收到通知时,我可以显示警报或某些视图,其中包含一些细节吗?我想在我的下一个应用程序中实现这个概念. 一些样本会对我有很大帮助.

Bla*_*icz 0

一种典型的模式是您注册应用程序的某个部分来“接收”或侦听特定意图。这样,您的应用程序可以在任意时间点唤醒,查看调用意图,并​​决定如何处理它(启动完整的应用程序、显示对话框或其他)。随之而来的一件好事是使用 AlarmManager 设置一个警报,该警报将在将来触发并唤醒您的应用程序(定期更新)。

这回答了你的问题了吗?我在我的应用程序中做了类似的事情,如果您愿意,我可以帮助您编写代码。

编辑来实现这一点,您将创建一个扩展 BroadcastReceiver 或 IntentService 的 java 类(具体取决于您是否希望该类分别在 ui 线程上运行或作为服务运行)。在下面的示例中,我定义了自己的意图操作键,但通常您会使用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);
 }

}
Run Code Online (Sandbox Code Playgroud)

无论该类是服务还是广播接收器,您都必须将其在应用程序中注册为“接收器”(意味着它可以接受某种类型的意图广播);这可以在 Java 代码或 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>
Run Code Online (Sandbox Code Playgroud)

当某种状态发生变化时,操作系统会广播不同的系统动作和通知;如果您想了解这些更改,请按照与上述相同的方式在清单中注册广播接收器或 IntentService,以获得不同的 Intent 操作(例如 ACTION_BATTERY_STATE_CHANGED)。所有文档都可以在android 开发网站上找到。我希望这能让您走上正确的道路。