Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. package com.example.alperen.nservice2;
  2.  
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.  
  11. Button startB,stopB;
  12. Intent intent;
  13. int count=0;
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19.  
  20. startB=(Button)findViewById(R.id.button);
  21. stopB=(Button)findViewById(R.id.button2);
  22.  
  23.  
  24. startB.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View view) {
  27.  
  28. intent = new Intent(MainActivity.this,MyService.class);
  29. startService(intent);
  30. }
  31. });
  32.  
  33. stopB.setOnClickListener(new View.OnClickListener() {
  34. @Override
  35. public void onClick(View view) {
  36. stopService(intent);
  37. }
  38. });
  39.  
  40. }
  41. }
  42.  
  43. package com.example.alperen.nservice2;
  44.  
  45. import android.app.Service;
  46. import android.content.Intent;
  47. import android.os.Bundle;
  48. import android.os.IBinder;
  49. import android.service.notification.NotificationListenerService;
  50. import android.service.notification.StatusBarNotification;
  51. import android.support.annotation.Nullable;
  52. import android.util.Log;
  53. import android.widget.Toast;
  54.  
  55. public class MyService extends NotificationListenerService{
  56.  
  57. @Override
  58. public void onCreate() {
  59. super.onCreate();
  60. System.out.println("********* SERVICE STARTED ***********");
  61. }
  62.  
  63. @Override
  64. public void onNotificationPosted(StatusBarNotification sbn) {
  65.  
  66. System.out.println("***** notification *****");
  67. String pack,title,text;
  68. Bundle extras;
  69.  
  70. try {
  71. pack = sbn.getPackageName();
  72. extras = sbn.getNotification().extras;
  73. title = extras.getString("android.title").toString();
  74. text = extras.getCharSequence("android.text").toString();
  75. }catch (Exception e)
  76. {
  77. System.out.println("**** HATA NOTIFYSERVICE CLASS ****");
  78. pack="empty1";
  79. title="empty1";
  80. text="empty1";
  81. System.out.println("**** "+pack+" ****");
  82. }
  83.  
  84. Log.i("Package",pack);
  85. Log.i("Title",title);
  86. Log.i("Text",text);
  87.  
  88. Toast.makeText(this,"title: "+title+" text: "+text,Toast.LENGTH_LONG).show();
  89. }
  90.  
  91. @Override
  92. public void onDestroy() {
  93. System.out.println("***** destroyed *****");
  94. super.onDestroy();
  95. }
  96. }
  97.  
  98. <?xml version="1.0" encoding="utf-8"?>
  99. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  100. xmlns:tools="http://schemas.android.com/tools"
  101. package="com.example.alperen.nservice2">
  102.  
  103. <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
  104. tools:ignore="ProtectedPermissions" />
  105.  
  106. <application
  107. android:allowBackup="true"
  108. android:icon="@mipmap/ic_launcher"
  109. android:label="@string/app_name"
  110. android:roundIcon="@mipmap/ic_launcher_round"
  111. android:supportsRtl="true"
  112. android:theme="@style/AppTheme">
  113. <activity android:name=".MainActivity">
  114. <intent-filter>
  115. <action android:name="android.intent.action.MAIN" />
  116. <category android:name="android.intent.category.LAUNCHER" />
  117. </intent-filter>
  118. </activity>
  119.  
  120. <service android:name=".MyService"
  121. android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
  122. <intent-filter>
  123. <action android:name="android.service.notification.NotificationListenerService" />
  124. </intent-filter>
  125. ></service>
  126.  
  127. </application>
  128.  
  129. // just delete this lines
  130. <intent-filter>
  131. <action android:name="android.service.notification.NotificationListenerService" />
  132. </intent-filter>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement