Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.alperen.nservice2;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends AppCompatActivity {
- Button startB,stopB;
- Intent intent;
- int count=0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- startB=(Button)findViewById(R.id.button);
- stopB=(Button)findViewById(R.id.button2);
- startB.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- intent = new Intent(MainActivity.this,MyService.class);
- startService(intent);
- }
- });
- stopB.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- stopService(intent);
- }
- });
- }
- }
- package com.example.alperen.nservice2;
- import android.app.Service;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.service.notification.NotificationListenerService;
- import android.service.notification.StatusBarNotification;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import android.widget.Toast;
- public class MyService extends NotificationListenerService{
- @Override
- public void onCreate() {
- super.onCreate();
- System.out.println("********* SERVICE STARTED ***********");
- }
- @Override
- public void onNotificationPosted(StatusBarNotification sbn) {
- System.out.println("***** notification *****");
- String pack,title,text;
- Bundle extras;
- try {
- pack = sbn.getPackageName();
- extras = sbn.getNotification().extras;
- title = extras.getString("android.title").toString();
- text = extras.getCharSequence("android.text").toString();
- }catch (Exception e)
- {
- System.out.println("**** HATA NOTIFYSERVICE CLASS ****");
- pack="empty1";
- title="empty1";
- text="empty1";
- System.out.println("**** "+pack+" ****");
- }
- Log.i("Package",pack);
- Log.i("Title",title);
- Log.i("Text",text);
- Toast.makeText(this,"title: "+title+" text: "+text,Toast.LENGTH_LONG).show();
- }
- @Override
- public void onDestroy() {
- System.out.println("***** destroyed *****");
- super.onDestroy();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- package="com.example.alperen.nservice2">
- <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
- tools:ignore="ProtectedPermissions" />
- <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>
- <service android:name=".MyService"
- android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
- <intent-filter>
- <action android:name="android.service.notification.NotificationListenerService" />
- </intent-filter>
- ></service>
- </application>
- // just delete this lines
- <intent-filter>
- <action android:name="android.service.notification.NotificationListenerService" />
- </intent-filter>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement