[Android] mobile guard incoming call display number, android incoming call display

Source: Internet
Author: User

[android] Mobile phone caller ID number attribution, android caller ID

Continue project N days ago

Open the service to monitor phone calls, query the database, and display the attribution

For details, please refer to this blog post: http://www.cnblogs.com/taoshihan/p/5331232.html

 AddressService.java

package com.qingguow.mobilesafe.service;

import com.qingguow.mobilesafe.utils.NumberQueryAddressUtil;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

/ **
 * Caller ID
 *
 * @author taoshihan
 *
 * /
public class AddressService extends Service {
    private TelephonyManager tm;
    private MyPhoneStateListener phoneStateListener;

    @Override
    public IBinder onBind (Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
    / **
     * Service creation
     * /
    @Override
    public void onCreate () {
        super.onCreate ();
        tm = (TelephonyManager) getSystemService (TELEPHONY_SERVICE);
        phoneStateListener = new MyPhoneStateListener ();
        tm.listen (phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    private class MyPhoneStateListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged (int state, String incomingNumber) {
            super.onCallStateChanged (state, incomingNumber);
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                String info = NumberQueryAddressUtil
                        .queryAddress (incomingNumber);
                Toast.makeText (getApplicationContext (), info, 1) .show ();
                break;
            default:
                break;
            }
        }
    }
    / **
     * Service destruction
     * /
    @Override
    public void onDestroy () {
        // TODO Auto-generated method stub
        super.onDestroy ();
        // Cancel monitoring
        tm.listen (phoneStateListener, PhoneStateListener.LISTEN_NONE);
        phoneStateListener = null;
    }
}
 

Setting Center, configure whether to enable the call attribution display

Directly use the combination control we defined before

    <com.qingguow.mobilesafe.ui.SettingItemView
        tsh: title = "Set the display number attribution"
        tsh: desc_on = "Setting display number attribution is on"
        tsh: desc_off = "Set display number attribution is closed"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: id = "@ + id / siv_show_address">
    </com.qingguow.mobilesafe.ui.SettingItemView>
 

Get the SettingItemView object, our custom control, set the state

Call the settingOnViewListener () method of the SettingItemView object, set the click event, and override the onClick method

Call the isChecked () method of SettingItemView object to get whether it is currently selected

To judge the status, call the setChecked () method of SettingItemView object, set the status, parameter: Boolean

Call the startService () method to start the service that monitors the status of the phone. Parameters: Intent object,

Call the stopService () method to close the service

 

Determine whether the current service is turned on, set the default selected state of the control

Create a new tool class ServicesUtils.java

Define a static method isServiceRunning (), passing in parameters: Context context, String service name

Call the getSystemService () method of the Context object to obtain the ActivityManager object, parameter: Context.ACTIVITY_SERVICE

Call the getRunningServices () method of the ActivityManager object to get the list of running services List, parameter: int max

for loop List collection, each is a RunningServiceInfo object

Call RunningServiceInfo.servie.getClassName () to get the String service class name, and if judged equal, return true

 

SettingActivity.java

package com.qingguow.mobilesafe;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.qingguow.mobilesafe.service.AddressService;
import com.qingguow.mobilesafe.ui.SettingItemView;
import com.qingguow.mobilesafe.utils.ServiceUtils;

public class SettingActivity extends Activity {
    private SettingItemView siv_item;
    private SharedPreferences sp;
    // Set whether to open the number attribution
    private SettingItemView showAddressBtn;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_setting);

        // Set the number attribution
        showAddressBtn = (SettingItemView) findViewById (R.id.siv_show_address);
        if (ServiceUtils.isRunningService (this,
                "com.qingguow.mobilesafe.service.AddressService")) {
            showAddressBtn.setChecked (true);
        } else {
            showAddressBtn.setChecked (false);
        }
        showAddressBtn.setOnClickListener (new OnClickListener () {
            @Override
            public void onClick (View arg0) {
                if (showAddressBtn.isChecked ()) {
                    showAddressBtn.setChecked (false);
                    stopService (new Intent (getApplicationContext (),
                            AddressService.class));
                } else {
                    showAddressBtn.setChecked (true);
                    startService (new Intent (getApplicationContext (),
                            AddressService.class));
                }
            }
        });

        siv_item = (SettingItemView) findViewById (R.id.siv_item);
        sp = getSharedPreferences ("config", MODE_PRIVATE);
        // Set the status based on the saved data
        boolean update = sp.getBoolean ("update", false);
        if (update) {
            siv_item.setChecked (true);
        } else {
            siv_item.setChecked (f
alse);
        }

        // Automatically updated click events
        siv_item.setOnClickListener (new OnClickListener () {
            @Override
            public void onClick (View arg0) {
                Editor editor = sp.edit ();
                if (siv_item.isChecked ()) {
                    // set unchecked
                    siv_item.setChecked (false);
                    editor.putBoolean ("update", false);
                } else {
                    // set selected
                    siv_item.setChecked (true);
                    editor.putBoolean ("update", true);
                }
                editor.commit ();
            }
        });
    }
}
 

 

ServicesUtils.java

package com.qingguow.mobilesafe.utils;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
/ **
 * Service tools
 * @author taoshihan
 *
 * /
public class ServiceUtils {
    / **
     * Determine whether a service is enabled
     * @param context
     * @param serviceName
     * @return
     * /
    public static boolean isRunningService (Context context, String serviceName) {
        ActivityManager am = (ActivityManager) context.getSystemService (Context.ACTIVITY_SERVICE);
        List <RunningServiceInfo> infos = am.getRunningServices (100);
        for (RunningServiceInfo info: infos) {
            String name = info.service.getClassName ();
            if (name.equals (serviceName)) {
                return true;
            }
        }
        return false;
    }
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.