【问题标题】:android broadcast receiver and service to get notificationandroid广播接收器和服务获取通知
【发布时间】:2016-09-13 06:03:31
【问题描述】:

您好,我在接收器类中使用了以下代码

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);
    }
 }

这里是我在服务类中使用的代码

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());

    }}

我创建了这段代码来获取通知。当我运行应用程序时,接收器类被触发,但它没有移动到服务类来调用通知。谁能说代码中有什么问题或告诉我如何使用广播获取通知详细教程中的接收器和服务

这是清单文件,并告诉我将代码准确放在哪里

<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>

请详细解释代码中的问题。请准确说明在哪里放置服务标签

【问题讨论】:

  • 在manifeast.xml中添加服务如:
  • 向我们展示您的清单文件。
  • 根据 developer.android.com/guide/components/services.html 服务生命周期没有回调 onStart() 方法,我认为在 onCreate() 中编写代码可能会有所帮助
  • @Aniruddha 我已经上传了清单文件。告诉我将服务标签准确放在哪里。我也尝试过应用程序强制关闭。
  • @KeerthivasanSelvarajan 检查我的答案。如果它仍然崩溃,然后发布错误。

标签: java android notifications


【解决方案1】:

你也必须在清单中declare service。 启动服务的意图被发送到系统,然后系统尝试使用清单文件中传递的信息找到正确的应用程序组件来处理这个特定的意图。

应该够了,不过不知道你有没有把service放在默认包里。

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

【讨论】:

  • Piotrpo 请告诉我在哪里放置服务标签。低于接收器标签或高于应用标签
  • 你能说清楚把服务标签放在哪里吗?当我把它放在应用程序取消赎回权时告诉我你会发生这种情况
  • 我已经添加了上下文。如果您在此处粘贴错误调用堆栈以防出现问题,将会很有帮助。
  • @piotrpo09 -13 14:58:48.212 23085-23085/com.example.ashwin.pillreminder11 E/时间对了:耶! 09-13 14:58:48.218 23085-23085/com.example.ashwin.pillreminder11 D/ActivityThread:BDC-RECEIVER 处理:0 / ReceiverData{intent=Intent { flg=0x114 cmp=com.example.ashwin.pillreminder11/。 AlarmReceiver(有附加功能)} packageName=com.example.ashwin.pillreminder11 resultCode=0 resultData=null resultExtras=null} 这些是我在放置服务标签后收到的错误通知没有到来
  • 上面的代码是否正确,否则我必须将服务类从 onstart 更改为 oncreate。如果是,你能告诉我代码
【解决方案2】:

您的清单应如下所示

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多