【问题标题】:NotificationExtension in Sony Smartwatch索尼智能手表中的通知扩展
【发布时间】:2013-10-21 09:49:37
【问题描述】:

有人知道如何在索尼智能手表中添加一次通知吗?我遵循了 SampleNotificationExtension。正如我在 SampleExtensionService 中所知道的,有 2 种方法与开始插入事件相关:

    /**
     * Start periodic data insertion into event table
     */
    private void startAddData() {
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, SampleExtensionService.class);
        i.setAction(INTENT_ACTION_ADD);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
                INTERVAL, pi);
    }

    /**
     * Cancel scheduled data insertion
     */
    private void stopAddData() {
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, SampleExtensionService.class);
        i.setAction(INTENT_ACTION_ADD);
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        am.cancel(pi);
    }

在输出中会出现通知,但它会每隔几秒重复一次。任何人都知道如何处理事件,以便我可以显示一次特定的通知?谢谢

【问题讨论】:

    标签: android android-intent notifications sony sony-smartwatch


    【解决方案1】:

    ExtensionService 像其他安卓服务一样工作。只是通过Context 实例向您的通知服务发送意图。

    public class SampleExtensionService extends ExtensionService {
        public static final String EXTENSION_KEY = "sample.app";
        public static final String EXTENSION_SPECIFIC_ID = "notification.id";
        public static final String INTENT_ACTION_ADD = "add";
        public static final String EXTRA_EVENT_CONTENT = "extra.eventcontent";
    
        public SampleExtensionService() {
            super(EXTENSION_KEY);
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            int retVal = super.onStartCommand(intent, flags, startId);
            if ((intent != null) && (intent.getAction().equals(INTENT_ACTION_ADD))) {
                if (intent.getExtras() != null) {
                    displayEvent(intent.getExtras());
                }
            }
            return retVal;
        }
    
        private void displayEvent(Bundle extras) {
            long sourceId = NotificationUtil.getSourceId(this, EXTENSION_SPECIFIC_ID);
            if (sourceId == NotificationUtil.INVALID_ID) {
                return;
            }
    
            ContentValues values = new ContentValues();
            values.put(Notification.EventColumns.DISPLAY_NAME, "New event");
            values.put(Notification.EventColumns.MESSAGE, extras.getString(EXTRA_EVENT_CONTENT));
            values.put(Notification.EventColumns.PERSONAL, 1);
            values.put(Notification.EventColumns.PUBLISHED_TIME, System.currentTimeMillis());
            values.put(Notification.EventColumns.SOURCE_ID, sourceId);
    
            getContentResolver().insert(Notification.Event.URI, values);
        }
    }
    

    从这个活动我们发送我们的服务意图

    public class MainActivity extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Button sendButton = (Button) findViewById(R.id.send_button);
            sendButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SampleExtensionService.class);
                    intent.setAction(SampleExtensionService.INTENT_ACTION_ADD);
    
                    Bundle extras = new Bundle();
                    extras.putString(SampleExtensionService.EXTRA_EVENT_CONTENT, "Some event content");
                    intent.putExtras(extras);
    
                    startService(intent);
                }
            });
        }
    }
    

    对于通知,您必须实现 getSourceRegistrationConfigurations()

    public class SampleRegistrationInformation extends RegistrationInformation {
        final Context mContext;
    
        protected SampleRegistrationInformation(Context context) {
            mContext = context;
        }
    
        @Override
        public int getRequiredNotificationApiVersion() {
            return 1;
        }
    
        @Override
        public ContentValues getExtensionRegistrationConfiguration() {
            <..>
        }
    
        @Override
        public ContentValues[] getSourceRegistrationConfigurations() {
            ContentValues sourceValues = new ContentValues();
            sourceValues.put(Notification.SourceColumns.ENABLED, true);
            sourceValues.put(Notification.SourceColumns.NAME, "Sample");
            sourceValues.put(Notification.SourceColumns.EXTENSION_SPECIFIC_ID, SampleExtensionService.EXTENSION_SPECIFIC_ID);
            sourceValues.put(Notification.SourceColumns.PACKAGE_NAME, mContext.getPackageName());
            return new ContentValues[]{sourceValues};
        }
    
    }
    

    【讨论】:

    • 嗨 Goncharov,你有一个例子,我应该在 if 语句中添加什么?
    • 实际上我无法在 myActivity 中开始运行该服务,因为它是我的手机应用程序和通知扩展程序之间的不同项目。但现在我已经解决了这个问题。感谢您的帮助
    【解决方案2】:

    通知继续重复的唯一原因是代码在 AlarmManager 中调用 setRepeating。如果您只想触发事件一次,您可以删除重复代码。要显示通知本身,请查看 SampleExtensionService.java 文件中的 addData() 方法。

    【讨论】:

    • 嗨 Marlin,如果我在 startAddData 中删除 setRepeating 的代码。我只有一个事件正在显示,其他事件没有显示。你知道为什么吗?
    • 这是有道理的,因为如果您不重复警报,那么它应该只添加一个事件。如果您希望显示其他事件,则每次要添加事件时都需要调用添加事件意图。
    • 谢谢你马林,我跟着你的方式。顺便说一句,我有振动的问题。为什么我收到的每条通知都没有振动?
    • 收到每条通知时手表都会振动。振动在其他扩展中是否有效?您是否尝试过重启手机和手表?您是否安装了最新版本的 SmartConnect 和 Host 应用?
    • 我用另一部手机检查过,它可以工作。在我的扩展中,我使用的是 Sony sdk 2.0。在我使用 android 版本为 2.3 的手机之前,我的手机没有振动。但是当我使用 4.3 版的手机时,它正在工作。你知道为什么吗?是手机的原因吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多