Do not understand different NotificationListenerService implementations - android

I'm trying to capture notifications posted in my Android 4.3
I've followed this example: http://www.kpbird.com/2013/07/android-notificationlistenerservice.html and it works seamlessly.
What I don't understand if I replace the NLService with my implementation which stands below, the service is never created and NLService class from example is created whenever I grant permissions from Security in Settings but my class does not.
public class ListenerService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "******** Service Created");
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}
with Manifest.xml
<service
android:name=".ListenerService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
Trying to know what's happening, I left NLService like my class, so it looks something like this:
public class NLService extends NotificationListenerService {
private String TAG = this.getClass().getSimpleName();
#Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "******** Service Created");
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}
with Manifest.xml
<service
android:name=".NLService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
And again, NLService works but ListenerService doesn't.
In my final attempt, I renamed ListenerService to NLListenerService, and then, it suddenly worked, printing "Service Created" when enabling the checkbox.
Can someone explain me what's happening here? :)

Related

How to detect a notification in android?

I am making some animation in android and I want to pause it whenever notification pops up in screen. For example sms, message from messenger, whatsApp, viber etc.
I don't need to know what is the type of notification or handle it somehow. I just need to know when it pops up so I can call my pause() method. How I can achieve this?
What you need is a notification listener service.
public class NotificationService extends NotificationListenerService {
Context context;
#Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
// Send a braoadcast with some data
Intent notificationMessage = new Intent("SomeData");
notificationMessage.putExtra("Hello", "World");
LocalBroadcastManager.getInstance(context).sendBroadcast(notificationMessage);
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg","Notification Removed");
}
}
You can recieve the Local broadcast in you activity as follows
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("Hello");
// Do something with the data
}
}, new IntentFilter("SomeData"));
}
}
Also register the service in manifest
<service
android:name=".NotificationService"
android:label="#string/app_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
you can find the official documentation here https://developer.android.com/reference/android/service/notification/NotificationListenerService
You can also find detailed demo here http://www.androiddevelopersolutions.com/2015/05/android-read-status-bar-notification.html

start IntentService always in background

this is my Broadcast_Receiver
public class Broadcast_Receiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, Notification_Intent_Service.class));
}
}
and this is my IntentService
public class Notification_Intent_Service extends IntentService {
public Notification_Intent_Service() {
super("GcmIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
Log.d("s","w");
}
}
at the end my manifest
<receiver
android:name=".Service.Broadcast_Receiver"
android:label="#string/app_name"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".Service.Notification_Intent_Service" />
i install the application and its didnt show any message tell me that's it run
did its want to start it from Activity ? if i must start it from Activity how can i make it auto start at any time without start it from Activity

Android Service donot get Bound

Hi I am new to android and exploring the services part.I could not bind the service using bind method.I could not figure out whether it is problem with bind method or on service connected.Someone please help me with it.Thanks in advance
Activity code:
public class MainActivity extends AppCompatActivity {
MyService pavan ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent i = new Intent(this,MyService.class);
this.startService(i);
System.err.println("before binding**************************");
this.bindService(i, con, Context.BIND_AUTO_CREATE);
this.unbindService(con);this.stopService(i);
}
public ServiceConnection con = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
pavan = ((MyService.localbinder)service).getservice();
System.err.println("here service gets binded");
Toast.makeText(getBaseContext(),"Service connected",Toast.LENGTH_LONG);
}
#Override
public void onServiceDisconnected(ComponentName name) {
Toast.makeText(getBaseContext(),"oops this is moment of Service disconnected",Toast.LENGTH_LONG);
}
};
}
Service code:
public class MyService extends Service {
public MyService() {
}
public class localbinder extends Binder{
public MyService getservice(){
return MyService.this;
}
}
IBinder ibinder = new localbinder();;
#Override
public IBinder onBind(Intent intent) {
return ibinder;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return Service.START_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
}
}
manifest code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.coolguy.pract">
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<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>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"></service>
</application>
</manifest>
Finally I got the problem in my code.Context.BIND_AUTO_CREATE automatically invokes startService().So here when I invoked bind method since service is already running it goes to on onStartCommand.So just do not use startservice with Context.BIND_AUTO_CREATE flag.

onNotificationPosted never called

I'm trying to implement a feature which would read the notification and it seems that I've a bug in it because the onNotificationPosted() of the NotificationListenerService is never called. I configured the manifest, modified the security settings properly and even started the service manually but it did nothing. Can it be that testing it in an emulator is not the best way to proceed.
Here is the Notification getter class:
/**
* Created by laurentmeyer on 28/02/15.
*/
public class NotificationListener extends NotificationListenerService {
public NotificationListener(){
Log.d("Notification", "Created");
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
Notification mNotification = sbn.getNotification();
Intent intent = new Intent("Msg");
Log.d("Notification", "Received");
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
}
#Override
public void onCreate() {
super.onCreate();
Log.d("Notification", "created");
}
#Override
public void onDestroy() {
super.onDestroy();
Log.d("Notification", "destroyed");
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
}
}
(Sorry for the incorrect indentation)
This is my Fragment which should respond to the broadcasts:
public class VerificationFragment extends BaseFragment implements SMSReceivedCallbacks {
public void onNotificationReceived(int code) {
ed.setText("OK");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(new Receiver(), new IntentFilter("Msg"));
}
// A bunch of other useless stuff
private class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
onNotificationReceived(0);
}
}
}
The Manifest:
<service
android:name=".NotificationListener"
android:label="#string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
To summarise, I've:
02-28 11:40:53.236 6585-6585/com.******.laurentmeyer.****** D/Notificationīš• Created
02-28 11:40:53.236 6585-6585/com.******.laurentmeyer.****** D/Notificationīš• created
and then nothing. I already looked at all the threads on SO but maybe missing something really simple.
Enable Notification Access in your settings:
if (Settings.Secure.getString(this.getContentResolver(), "enabled_notification_listeners").contains(getApplicationContext().getPackageName())) {
//Add the code to launch the NotificationService Listener here.
} else {
//Launch notification access in the settings...
getApplicationContext().startActivity(new Intent(
"android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
}

Unable to start service Intent { cmp=com.marie.mainactivity/.BackgroundService }: not found

I've been studying from the book "Pro Android 2." I'm working through a Service example that consists of two classes: BackgroundService.java and MainActivity.java. The MainActivity claims (erroneously?) it starts the Service as indicated by output to logcat from the Log.d call below:
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "starting service");
Button bindBtn = (Button)findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent backgroundService = new Intent(MainActivity.this, com.marie.mainactivity.BackgroundService.class);
startService(backgroundService);
}
});
Button unbindBtn = (Button)findViewById(R.id.unbindBtn);
unbindBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
stopService(new Intent(MainActivity.this, BackgroundService.class));
}
});
}
}
What puzzles me is the UI provides two buttons: Bind and UnBind as shown above. But according to the documentation if onBind() as shown below returns null that indicates you don't want to allow binding. But as shown above the onClick() method of (the Bind button) bindBtn.setOnClickListener(new OnClickListener() calls startService(backgroundService) which gives this error:"Unable to start service Intent { cmp=com.marie.mainactivity/.BackgroundService }: not found"
public class BackgroundService extends Service {
private NotificationManager notificationMgr;
#Override
public void onCreate() {
super.onCreate();
notificationMgr = NotificationManager)getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("starting Background Service");
Thread thr = new Thread(null, new ServiceWorker(), "BackgroundService");
thr.start();
}
class ServiceWorker implements Runnable
{
public void run() {
// do background processing here...
//stop the service when done...
//BackgroundService.this.stopSelf();
}
}
#Override
public void onDestroy()
{
displayNotificationMessage("stopping Background Service");
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private void displayNotificationMessage(String message)
{
Notification notification = new Notification(R.drawable.note, message, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, "Background Service", message, contentIntent);
notificationMgr.notify(R.id.app_notification_id, notification);
}
}
I don't understand the point of this example. If onBind() returns null what's the point of having a Bind button (bindBtn)? I thought the point was to show how to start a BackgroundService. But it doesn't seem to work unless I'm missing something.
I should add I have added to my AndroidManifest.xml:
<service android:name=".BackgroundService"></service>
as follows:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name=".BackgroundService"></service>
</activity>
</application>
Remove the service from inside the activity. It is at the same level as the activity within the application. Eg:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BackgroundService"></service>
</application>

Categories

Resources