Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------- AndroidManifest.xml -----------------------
- <?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.sgl.broadcastreceiver">
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.READ_CALL_LOG" />
- <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"
- tools:ignore="GoogleAppIndexingWarning">
- <receiver
- android:name=".CallReceiver"
- android:enabled="true"
- android:exported="true">
- <intent-filter>
- <action android:name="android.intent.action.PHONE_STATE"/>
- <action android:name="android.intent.action.CALL_LOG"/>
- </intent-filter>
- </receiver>
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- // --------------------------- MainActivity.java ----------------------------
- package com.sgl.broadcastreceiver;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.app.ActivityCompat;
- import androidx.core.content.ContextCompat;
- import android.Manifest;
- import android.app.Activity;
- import android.content.Context;
- import android.content.IntentFilter;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager;
- import android.os.Bundle;
- import android.telephony.TelephonyManager;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListView;
- import android.widget.TextView;
- import java.util.ArrayList;
- import java.util.Set;
- public class MainActivity extends AppCompatActivity {
- Context context;
- private ArrayList<String> callsList;
- private static final String SHARED_PREFERENCES = "sharedPreferences";
- private static int index = 0;
- private ListView incomingList;
- SharedPreferences sharedPreferences;
- SharedPreferences.Editor editor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- setPointer();
- }
- private void setPointer() {
- this.context = this;
- sharedPreferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
- editor = sharedPreferences.edit();
- callsList = new ArrayList<>();
- if((ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) &&
- (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED)) {
- ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
- ActivityCompat.requestPermissions((Activity) context, new String[] {Manifest.permission.READ_CALL_LOG}, 1);
- } else {
- // Permission is already granted
- incomingList = findViewById(R.id.incoming_calls_list);
- if(index == 0) clearSPList();
- registerReceiver(new CallReceiver(), new IntentFilter(TelephonyManager.EXTRA_INCOMING_NUMBER));
- Bundle extras = getIntent().getExtras();
- if(extras != null) {
- String phNumber = extras.getString("phoneNumber");
- saveData(phNumber);
- loadData();
- }
- incomingList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- if(sharedPreferences.getAll().size() > 0) {
- editor.remove(((TextView)view.findViewById(R.id.callNumber)).getText().toString());
- editor.commit();
- index--;
- loadData();
- }
- }
- });
- }
- }
- private void saveData(String data) {
- int x = Integer.parseInt(sharedPreferences.getString(data, "0"));
- x += 1;
- editor.putString(data, Integer.toString(x));
- index++;
- editor.commit();
- }
- private void clearSPList() {
- editor.clear();
- editor.commit();
- callsList.clear();
- }
- private void loadData() {
- callsList.clear();
- callsList.addAll(sharedPreferences.getAll().keySet());
- for(int i = 0; i < callsList.size(); i++) {
- if(Integer.parseInt(sharedPreferences.getString(callsList.get(i), "-1")) > 1) {
- callsList.set(i, callsList.get(i) + " (" + sharedPreferences.getString(callsList.get(i), "-1") + ")");
- }
- }
- CallAdapter callAdapter = new CallAdapter(context, callsList);
- incomingList.setAdapter(callAdapter);
- }
- }
- // ---------------------- CallReceiver.java -----------------------------
- package com.sgl.broadcastreceiver;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.telephony.TelephonyManager;
- public class CallReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
- String phNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
- if(phNumber == null) return;
- Intent intent1 = new Intent(context, MainActivity.class);
- intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- intent1.putExtra("phoneNumber", phNumber);
- context.startActivity(intent1);
- }
- }
- }
- //------------------------ CallAdapter.java -----------------------------
- package com.sgl.broadcastreceiver;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- import java.util.ArrayList;
- class CallAdapter extends BaseAdapter {
- Context context;
- ArrayList<String> callsList;
- public CallAdapter(Context context, ArrayList<String> callsList) {
- this.context = context;
- this.callsList = callsList;
- }
- @Override
- public int getCount() {
- return callsList.size();
- }
- @Override
- public Object getItem(int position) {
- return callsList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- final View singleCall = LayoutInflater.from(context).inflate(R.layout.call_layout, null);
- final TextView number = singleCall.findViewById(R.id.callNumber);
- number.setText(callsList.get(position));
- return singleCall;
- }
- }
- // ------------------------ activity_main.xml ------------------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/title"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="6"
- android:background="#3949AB"
- android:gravity="center"
- android:text="@string/call_receiver"
- android:textColor="#FAFFFFFF"
- android:textSize="36sp" />
- <ListView
- android:id="@+id/incoming_calls_list"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1" />
- </LinearLayout>
- </androidx.constraintlayout.widget.ConstraintLayout>
- // ---------------------- call_layout.xml -----------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/call_layout"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="20dp"
- android:background="#3C00ACC1"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/imgCall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:layout_weight="1"
- android:contentDescription="@string/img_call"
- android:padding="5dp"
- app:srcCompat="@drawable/call" />
- <TextView
- android:id="@+id/callNumber"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="3"
- android:gravity="center"
- android:text="@string/_5555555555"
- android:textSize="30sp" />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement