简体   繁体   English

android广播接收器和服务获取通知

[英]android broadcast receiver and service to get notification

hi i have used the following code in receiver class 嗨,我在接收器类中使用了以下代码

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("the time is right","yay!");
        Intent i = new Intent(context, AlarmServie.class);
        context.startService(i);
    }
 }

here i the code which i used in service class 这里是我在服务类中使用的代码

public class AlarmServie extends Service {
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e("onStart:","came" );
     /*  NotificationManager notifyman = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent main_activity = new Intent(this.getApplicationContext(), MainActivity.class);
        PendingIntent o = PendingIntent.getActivity(this, 0, main_activity, 0);

        /*Notification noti = new Notification.Builder(this)
                .setContentTitle("Reminder to Pill")
                .setContentText("Click for info")
                .setAutoCancel(true)
                .setContentIntent(o)
                .build();*/
        NotificationManager nm = (NotificationManager) this.getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        Intent in = new Intent(this.getApplicationContext(), MainActivity.class);
        PendingIntent pending = PendingIntent.getactivity(this, 0, in, 0);
        NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(AlarmServie.this);
        mBuilder.setContentTitle("Pill Reminder");
        mBuilder.setContentText("CLick here to View");
        //mBuilder.setSound(sound);

        TaskStackBuilder ts=TaskStackBuilder.create(this);
        ts.addParentStack(MainActivity.class);
        nm.notify(9999,mBuilder.build());

    }}

i created this code to get notification.when i run the app the receiver class is triggered but it is not moving to service class to invoke notification.can anyone say whats wrong in the code or say me how to get notification using broadcast receiver and service in a detailed tutorial 我创建此代码以获取通知。当我运行应用程序时,接收器类被触发但它没有移动到服务类来调用notification.can任何人都说代码中有什么错误或者说我如何使用广播接收器和服务获取通知在详细的教程中

here is the manifest file and say me where to put the code exactly 这是清单文件,并告诉我在哪里准确地放置代码

<uses-permission android:name="android.permission.VIBRATE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />



            </intent-filter>
        </activity>
        <receiver android:name=".AlarmReceiver"></receiver>
        <activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>

please explain in detail what the problem in code.please tell exactly where to put the service tag 请详细解释code.pyase中的问题,确切地说明服务标签的放置位置

You must declare service in manifest also. 您还必须在清单中声明服务 Intent to start service is sent to system then system tries to find right application component to handle this particular intent using information passed in manifest files. 启动服务的意图被发送到系统,然后系统尝试使用清单文件中传递的信息找到正确的应用程序组件来处理此特定意图。

It should be enough, but I'm not sure if you put service in the default package. 它应该足够了,但我不确定你是否将服务放在默认包中。

<application>
    ....
    <service android:name=".AlarmServie"/>
    ....
</application>

Your Manifest should look something like this 你的清单看起来应该是这样的

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />



        </intent-filter>
    </activity>

    //here is your service
     <service android:name=".AlarmServie" android:enabled="true"/>

    <receiver android:name=".AlarmReceiver"></receiver>
    <activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
 App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM