0

「X」分ごとにWebサービスを参照し、応答が「OK」の場合は通知バーに通知を生成するバックグラウンドサービスを作成しようとしています

Android サービスかどうかはわかりません o ブロードキャスト レシーバーが必要なものかどうか

とにかくありがとう

4

3 に答える 3

1

X分ごとにWebサービスからページをプルしないでください-これはバッテリーを大量に消費します。代わりにGoogleCloudMessaging(以前のC2DM-Cloud To Device Messaging)を使用して情報の更新を取得します-非常に軽量で非常に軽量です効率的で、すべてのアプリケーションに対して1つの既存のデータ接続を共有します。

于 2012-11-07T04:18:40.863 に答える
1

NotificationManagerはあなたが探しているもののようです。

于 2012-11-07T03:50:43.223 に答える
0

まず最初に、USER_PRESENT アクション (ユーザーが画面のロックを解除したとき) または BOOT_COMPLETED (携帯電話が OS およびその他すべてのもののロードを完了したとき) に反応するブロードキャスト レシーバー (独自のクラス内、およびマニフェスト内のコードのビット) が必要です。 . Boot は Broadcast をアクティブにし、Broadcast はサービスを開始します。サービスの onStartCommand() メソッド内で、X 分ごとに Web サービスへの接続を実行します。Web サービスに接続するには、サービスに AsyncTask を実装する必要があります。そのタスクの onCompleted() メソッドで、通知を呼び出します。

マニフェスト:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver android:name="classes.myReceiver" >        
             <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>


        <service  android:name="classes.myService">        
        </service>

クラス:

public class myReceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context ctx, Intent intent) {     
        Intent service = new Intent(ctx, myService.class);
        if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)||intent.getAction().equals(Intent.ACTION_USER_PRESENT)||intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            ctx.startService(service);
            }
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            ctx.stopService(service);  
            }
        }
}  

サンプル通知

    private void showNotification() {               
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationBuilder = new NotificationCompat.Builder(this);
        notificationBuilder.setContent(getNotificationContent());
        notificationBuilder.setOngoing(true);

            notificationBuilder.setTicker("Hello");
            notificationBuilder.setSmallIcon(R.drawable.notif_icon);

        mNotificationManager.notify(R.id.notification_layout, notificationBuilder.build()); 
        }

 private RemoteViews getNotificationContent() {  

            RemoteViews notificationContent = new RemoteViews(getPackageName(), R.layout.keyphrase_recogniser_notification);
        notificationContent.setTextViewText(R.id.notification_title, "title");
            notificationContent.setTextViewText(R.id.notification_subtitle, "subtitle");
return notificationContent;
            }

これは幅広いガイドです。より具体的なコードが必要な場合はお知らせください。

于 2015-03-19T14:50:29.787 に答える