1

私はどのAndroidデバイスのアンドロイドのステータスバーに固定ツールバーを実装することができるのだろうかと思っていました。 私は下に表示された通知のボタンについて話しています。例を以下に示します。ユーザーがアプリケーションを開いていなくても実行できるのですか?ステータスバーパネルのAndroid通知ツールバー?

誰かが正しい方向に私を向けることができますか?ライブラリがありますか?提供されているネイティブのアンドロイドライブラリを使用してこれを実装できますか?

enter image description here

+0

ブートReceiverで起動されたカスタム 'View'を使って進行中の' Notification'を作成することができます。ライブラリが不要です。 –

+1

私に例や参考リンクをお願いできますか? :) @MikeM。 – Dinuka

答えて

1

は、単純な例として、次のコードは、Buttonとの継続的なNotificationアプリを起動するには、ブート時に発行されます。

まず、マニフェストでBOOT_COMPLETEDブロードキャストを取得する許可をリクエストし、それを処理するReceiverを登録します。

<manifest ...> 

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    ... 

    <application ...> 
     ... 

     <receiver android:name=".BootReceiver"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

BootReceiverは、単にこの例でMainActivityで定義されているstatic方法を用いNotificationを発行します。

public class BootReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     MainActivity.setNotification(context, true); 
    } 
} 

setNotification()方法は、以下のシンプルなレイアウトを使用してNotificationためRemoteViewsインスタンスを作成し、アプリの起動IntentButtonPendingIntentを設定します。

public static void setNotification(Context context, boolean enabled) { 
    NotificationManager manager = 
     (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); 

    if (enabled) { 
     final RemoteViews rViews = new RemoteViews(context.getPackageName(), 
                R.layout.notification); 

     final Intent intent = context.getPackageManager() 
      .getLaunchIntentForPackage(context.getPackageName()); 

     if (intent != null) { 
      PendingIntent pi = PendingIntent.getActivity(context, 
                 0, 
                 intent, 
                 0); 

      rViews.setOnClickPendingIntent(R.id.notification_button_1, pi); 
     } 

     Notification.Builder builder = new Notification.Builder(context); 
     builder.setContent(rViews) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setWhen(0) 
      .setOngoing(true); 

     manager.notify(0, builder.build()); 
    } 
    else { 
     manager.cancel(0); 
    } 
} 

Notificationのレイアウトは、単に水平LinearLayoutImageViewButtonあります。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView android:id="@+id/notification_image" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_marginRight="10dp" 
     android:src="@drawable/ic_launcher" /> 

    <Button android:id="@+id/notification_button" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:text="Button" /> 

</LinearLayout> 

API 3.1以降、あなたは停止状態からそれを持って来るために、インストール後に少なくとも一度あなたのアプリを起動する必要があります、ということに注意してください。それまでは、BootReceiverは配信されません。

+0

そのままではありません。どのように処理したいのか分かりませんでしたが、実行する必要がある場合は、2番目の引数として 'true'を使って' setNotification() 'メソッドを呼び出すだけです。例えば、 'MainActivity'が始まるときにそれを表示したいなら、' MainActivity'の 'onCreate()'メソッドで次の行を呼び出します: 'setNotification(this、true);'。 'Notification'を隠したいときはいつでも、それを' false'を第2引数として呼び出します。 –

+1

私は、ユーザーが現在のセッション中にあなたのアプリケーションをまだ動かしていなくても、あなたがそれを絶えず表示したいと思うように、「ユーザーがアプリケーションを開いていないとき」を取った。それはブートレシーバが処理するものです。あなたのアプリケーションをインストールして、それを一度実行し、再起動すると、起動プロセスが完了した直後に表示されます。それは少し時間がかかることがあります。 –

+0

私はアプリを起動するときに私は通知を取得していない:/あなたのコードは正しいマイクですか? – Dinuka