Implementation of no interface Activity or APP

Recently, I was working on an Issue that Google Voice calls the APP to set the alarm clock. Looking at the official documents, I found that Google Voice needs to start an Activity when sending an Intent to the App. Can I use Service or broadcast to receive the Intent? The answer is no, there is a problem with using ordinary Activity: every time the voice sends an Intent to the Activity of the APP, an Activity must be started, and when the return information from the Activity cannot be received, Google will continue to send the Intent until Received return information. If the Activity does not immediately return information to the Activity, the Activity will continue to start, and even cause the system to crash. In addition to the operation through Google Voice, the Activity of the APP has to be activated to make it visible, and the experience is very bad. So I want to write an Activity without an interface, so that the java code of this Activity can be executed, and the interface can be unrealistic. This situation is achievable.

1. First create a new Activity, make it inherit Activity, can not make other, such as AppCompatActivity (with actionbar or toolbar) theme and activity do not match the app running error.

public class MainActivity extends Activity

2. Add the theme of this Activity in the manifest file AndroidManifest.

android:theme="@android:style/Theme.NoDisplay"

If you need to make a non-interface APP, you can hide the icon and background tasks of the APP. If not, click on the application to give the user the illusion of "application not responding".

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleInstance" 
android:excludeFromRecents="true">   //后台任务图标设置为true
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
   <!--<category android:name="android.intent.category.LAUNCHER" />--> //桌面启动图标去除
</intent-filter>
</Activity>

3. Finally, go back to the Activity, rewrite the onResume() method, and kill the Activity in the onResume() method (depending on the function you implement.

@Override
    protected void onResume() {
        super.onResume();
        this.finish();
    }

The function of setting alarms and timers by Google voice is not used much in China, and it is difficult to find a demo. As we all know, because Google services are not available in China, domestic developers rarely do this function. After the function is implemented, I will write a Demo for your reference.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686228&siteId=291194637
Recommended