In Android, how to implement custom broadcast can only receive the specified app, androidapp

Source: Internet
Author: User

In Android, how to implement custom broadcast can only receive the specified app, androidapp

I went to the interview again today, but I won't talk about the specific company, because I noted the names of those companies in my previous blog, as a result, people sent me a private message saying that I leaked their questions. Okay, I am wrong... In fact, when we are at work, we can face each other in our free time, because the interview has many advantages:

First: You know the specific address of the company. You will also have a topic when talking to your friends later.

Second: This is very important. Let's take a look at the interview questions of other companies (some companies are still using the pen exam now. I am speechless, and the questions are all obtained from the Internet, which is not interesting, so as long as I see all the passing questions, I personally feel that Alibaba and Didi are still good, and they will ask you the specific problems they encountered in the project, if you ask how to solve the problem, you are not sure about the answer. You are tested by experience)


As mentioned above, let's get started with the question:

Question: How to Implement custom broadcast in Android can only receive a specified app

Thought: in fact, the answer I first came up with was to add access permissions to the broadcast, but I have previously granted access permissions to the Activity, so I'm not sure if this is correct, but I think the four components in Android should be the same. I was blinded by the result. Haha ~~


We can open an application's Activity/Service externally or send a broadcast by using the Android API or the adb command. Therefore, this is not safe for an application. That is, permission is a protection mechanism.


Solution:

In fact, there is no technology at all, that is, the Android can customize permissions, add a layer of protection for access to the four components, not to mention, directly go to the Code:


Send broadcast:

package com.tt.test;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Main extends Activity {    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        ((Button)findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {   Intent i = new Intent("COM.MESSAGE");   i.addCategory("receiver");   i.putExtra("message", "haha");   sendOrderedBroadcast(i, "xvtian.gai.receiver");}});    }}
AndroidManifest. xml:

<uses-permission android:name="xvtian.gai.receiver" ></uses-permission>  <permission android:protectionLevel="normal" android:name="xvtian.gai.receiver"></permission>

Receive broadcast:

package com.tt.receiver;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class Receiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {Log.d("TAG", "receiver intent:" + intent.toString());}}
AndroidManifest. xml

<uses-permission android:name="xvtian.gai.receiver" ></uses-permission>
<receiver android:name=".Receiver" android:permission="xvtian.gai.receivers">       <intent-filter>         <action android:name="COM.MESSAGE" />         <category android:name="receiver" />       </intent-filter></receiver>


Simple, there is no technology at all. Similarly, we can protect the permissions of other components (most of which are to add permissions to access the Activity ), of course, the applications I normally develop do not seem to use these things, but if you want to learn, you should find those security applications, such as the 360 mobile guard, after decompiling, open his AndroidManifest. xml files, you will be shocked, and you can also learn a lot. Haha ~~


Demo:

Http://download.csdn.net/detail/jiangwei0910410003/7973867





How does android customize resident broadcast?

Is the problem solved? The general procedure should be to send a broadcast in an activity or service (you can define its name and then sendbroadcast () in another
Activity receives broadcasts. Generally, it registers and receives messages in oncrete (), and implements what you want to do when receiving broadcasts in the rewritten onreceive () method. If you want to learn more, you can continue to ask.
 
Can the android APP receive System Broadcasts when it is not started?

1. You must register a broadcast and register it with the system in the service.
2. It must be implicitly registered, that is, registered in AndroidManifest.
3. You must register a service and register it in AndroidManifest.
4. Run a program

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.