Android
 Computer >> コンピューター >  >> プログラミング >> Android

ブロードキャストレシーバーからのAndroid通知を開始および停止しますか?


この例は、ブロードキャストレシーバーからのAndroid通知の開始と停止について示しています

ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。

ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。

<? xml version = "1.0" encoding= "utf-8" ?>
<RelativeLayout xmlns: android = "https://schemas.android.com/apk/res/android"
   xmlns: tools = "https://schemas.android.com/tools"
   android :layout_width = "match_parent"
   android :layout_height = "match_parent"
   android :padding = "16dp"
   tools :context = ".MainActivity" / >

ステップ3 −次のコードをsrc/MainActivityに追加します。

package app.tutorialspoint.com.notifyme ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super .onCreate(savedInstanceState) ;
      setContentView(R.layout. activity_main ) ;
   }
}

ステップ4 −次のコードをsrc / USBStateReceiver

に追加します
package app.tutorialspoint.com.notifyme ;
import android.annotation. SuppressLint ;
import android.app.Notification ;
import android.app.NotificationChannel ;
import android.app.NotificationManager ;
import android.content.BroadcastReceiver ;
import android.content.Context ;
import android.content.Intent ;
import android.support.v4.app.NotificationCompat ;
import android.util.Log ;
public class USBStateReceiver extends BroadcastReceiver {
   public static final String NOTIFICATION_CHANNEL_ID = "10001" ;
   private final static String default_notification_channel_id = "default" ;
   boolean connected = true;
   @SuppressLint ( "UnsafeProtectedBroadcastReceiver" )
   @Override
   public void onReceive (Context context , Intent intent) {
      NotificationCompat.Builder builder = new NotificationCompat.Builder(context , default_notification_channel_id ) ;
      builder.setContentTitle( "USB - Notification" ) ;
      String action = intent.getAction() ;
      Log. e ( "USB" , action) ;
      assert action != null;
      builder.setContentText( "Connected" ) ;
      builder.setSmallIcon(R.drawable. ic_launcher_foreground ) ;
      builder.setAutoCancel( true ) ;
      builder.setChannelId( NOTIFICATION_CHANNEL_ID ) ;
      Notification notification = builder.build() ;
      NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context. NOTIFICATION_SERVICE ) ;
      if (android.os.Build.VERSION. SDK_INT > = android.os.Build.VERSION_CODES. O ) {
         int importance = NotificationManager. IMPORTANCE_HIGH ;
         NotificationChannel notificationChannel = new NotificationChannel( NOTIFICATION_CHANNEL_ID , "NOTIFICATION_CHANNEL_NAME" , importance) ;
         assert notificationManager != null;
         notificationManager.createNotificationChannel(notificationChannel) ;
      }
      assert notificationManager != null;
      if ( connected ) {
         notificationManager.notify( 1 , notification) ;
         connected = false;
      } else {
         notificationManager.cancel( 1 ) ;
         connected = true;
      }
   }
}

ステップ5 −次のコードをAndroidManifest.xmlに追加します

<? xml version = "1.0" encoding= "utf-8" ?>
<manifest xmlns: android = "https://schemas.android.com/apk/res/android"
   package = "app.tutorialspoint.com.notifyme" >
   <uses-permission android :name = "android.permission.VIBRATE" /Gt;
   <application
      android :allowBackup= "true"
      android :icon = "@mipmap/ic_launcher"
      android :label = "@string/app_name"
      android :roundIcon = "@mipmap/ic_launcher_round"
      android :supportsRtl = "true"
      android :theme= "@style/AppTheme" >
      <activity android :name = ".MainActivity" >
         <intent-filter>
            <action android :name = "android.intent.action.MAIN" />'
            <category android :name = "android.intent.category.LAUNCHER" / >
         </intent-filter>
      </activity>
      <receiver android :name = ".USBStateReceiver" >
         <intent-filter>
            <action android :name = "android.hardware.usb.action.USB_STATE" />
         </intent-filter>
      </receiver>
   </application>
</manifest>

アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[実行]アイコンをクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します-

ブロードキャストレシーバーからのAndroid通知を開始および停止しますか?


  1. アプリがAndroid携帯のバッテリーを消耗するのを見つけて止める方法

    バッテリーの寿命を維持することは、携帯電話が実際に「モバイル」であり続けるための鍵です。いくつかの新しいアプリをダウンロードして、バッテリーの寿命が大幅に低下し始めたことを確認することほど悪いことはありません。幸いなことに、どのアプリが最もバッテリーを消耗しているかを確認できる方法があり、バッテリーの寿命をもう一度取り戻すことができます。 エネルギードレイナーを見つける方法 Androidのバッテリー設定の使用 どのアプリがバッテリーに最大の負担をかけているのかを確認するには、まず[設定]にアクセスします。これは通常、アプリの全リストにアクセスして[設定]を選択することで実行されます。

  2. デスクトップとAndroidでChrome通知を停止する方法

    多くのWebサイトは、プッシュ通知を使用して更新をPCまたはスマートフォンに直接送信します。これは、新しい記事や投稿へのコメントを見逃すことはないため、便利な方法です。ただし、すべてのWebサイトに通知の送信を許可すると、すぐに処理しきれなくなる可能性があります。ここでは、Chromeの通知が届かないようにする方法を説明します。 デスクトップでChrome通知を無効にする PC(Windows / macOS)のChrome通知は、ブラウザの設定から直接簡単に無効にできます。 Chromeのメインメニュー(右上隅にある3つのドット)をクリックして、[設定]を選択します。 次に、[プライバ