无法在android studio中显示通知

时间:2016-03-03 07:19:13

标签: java android

我无法显示通知。在MainaActivit中,我没有调用startService方法。我必须只在后台服务中显示通知,即电话启动通知立即显示。当我调试错误显示在控制台中,我在下面提到。

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

BeaconService.Java

public class BeaconService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        showNotification();
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
    }
    private void showNotification() {
        NotificationCompat.Builder mBuilder =
                (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_loc)
                        .setContentTitle("Welcome to Brillio")
                        .setContentText("Hello Mansur, Welcome to Brillio.")
                        .setPriority(2)
                        .setOnlyAlertOnce(false);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        mBuilder.setSound(alarmSound);
        mBuilder.setOngoing(true);
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(2001, mBuilder.build());
    }
}

BeaconReceiver.java

public class BeaconReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, BeaconService.class);
        context.startService(startServiceIntent);
    }
}

menifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service
            android:name=".BeaconService"
            android:enabled="true"
            android:exported="true" />
        <receiver android:name=".BeaconReceiver">
            <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

控制台

中显示错误
DEVICE SHELL COMMAND: am start -D -n "com.example.serviceexamp/com.example.serviceexamp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet.
open: Permission denied
open: Permission denied

1 个答案:

答案 0 :(得分:0)

Manifest文件中的Service类名称与您在检查时定义的Service类不同。

实际服务类:公共类BeaconService扩展服务

Manifest中定义的服务类:android:name =&#34; .MyService&#34;