I just want to launch activity via Secret Code .
Although , there are many solutions available for this problem but none of them worked for me. It seems like BroadcastReceiver is not listening .
I am using Mi4i MIUI 7.
Here's my code. Please Help!!
MainActivity.java
package com.example.jatin.myapplication2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I have even tried the below commented code , but that didn't work.
MySecretCodeReceiver.java
package com.example.jatin.myapplication2;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Jatin on 05-Aug-16.
*/
public class MySecretCodeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SECRET_CODE")) {
String uri = intent.getDataString();
String sep[] = uri.split("://");
if (sep[1].equalsIgnoreCase("1234"))
{
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("com.example.jatin.myapplication2.MainActivity");
context.startActivity(launchIntent);
}
else if (sep[1].equalsIgnoreCase("5678")) {
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage("net.one97.paytm");
context.startActivity(launchIntent);
}
// Intent i = new Intent(context, MainActivity.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// setResultData(null);
// context.startActivity(i);
}
}
}
I have also tried putting android:host="1234" in manifest ,still not getting desired results
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>
<receiver android:name="receivers.MySecretCodeReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.PROCESS_OUTGOING_CALLS">
<intent-filter>
<action android:name="android.provider.Telephony.SECRET_CODE" />
<data android:scheme="android_secret_code" />
</intent-filter>
</receiver>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Got it ....
We have to register our MySecretCodeReceiver as a <receiver> within the <application> element in the AndroidManifest.xml file.
And , Its Done. :)
Related
I want to start a service after reboot. The problem I have is that this does not happen every time (at least the first 20 minnutes). I have study many questions into stackoverflow and try a number of the provided solutions however sometimes the service does not automaticaly start after reboot.
Also I have to add another parameter this of the foreground service for android versions O and above.
Could someone give me any advice?
AndroidManifest.xml
.... <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".App"
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"
>
<activity
android:name=".Activities.MainActivity"
....
</activity>
<activity
...
</activity>
<receiver android:name=".Helpers.BootCompletedIntentReceiver" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<!-- I think that this is also not necessery <category android:name="android.intent.category.DEFAULT" /> -->
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<service
android:name=".Services.myService"
android:enabled="true"
android:exported="true"></service>
</application>
BroadcastReciever
package com.abc.Helpers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.abx.Activities.MainActivity;
import com.abc.Services.myService;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
private static final String TAG = "MyBroadcastReceiver";
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, myService.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(context,serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
I also found in a comment here that reciever should be registered into an activity of the application and this is the code in Mainactivity. Do you agree?
#Override
protected void onCreate(Bundle savedInstanceState) {
...
final ComponentName onBootReceiver = new ComponentName(getApplication().getPackageName(), BootCompletedIntentReceiver.class.getName());
if(getPackageManager().getComponentEnabledSetting(onBootReceiver) != PackageManager.COMPONENT_ENABLED_STATE_ENABLED)
getPackageManager().setComponentEnabledSetting(onBootReceiver,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
..
}
I have two main classes in my application for activity launch as.
MainActivity.Java
package com.connect;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
private PolicyManager policyManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
policyManager = new PolicyManager(this);
System.out.println("Hello");
if (!policyManager.isAdminActive()) {
Intent activateDeviceAdmin = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdmin.putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN,
policyManager.getAdminComponent());
activateDeviceAdmin
.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"After activating admin, you will be able to block application uninstallation.");
startActivityForResult(activateDeviceAdmin,
PolicyManager.DPM_ACTIVATION_REQUEST_CODE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (resultCode == Activity.RESULT_OK
&& requestCode == PolicyManager.DPM_ACTIVATION_REQUEST_CODE) {
// handle code for successfull enable of admin
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Droidian.java
package com.connect;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import java.io.IOException;
public class Droidian extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
PackageManager i = getApplicationContext().getPackageManager();
i.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
if (isMyServiceRunning() == false) {
startService(new Intent(getApplicationContext(), DroidianService.class));
Log.i("com.connect", "startService");
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (DroidianService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
}
After executing MainActivity.java , Droidian.java should run as this is the class which would be doing all the function. I don't know how to call the other class in MainActivity.java class. Here is my Manifest file snippet.
Android Manifest.xml
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.connect"
android:versionCode="1"
android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="18" tools:ignore="OldTargetApi"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<supports-screens android:resizeable="true"
android:largeScreens="true"
android:xlargeScreens="true"
/>
<application
android:allowBackup="false"
android:icon="#drawable/launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:persistent="true">
<activity
android:name="com.connect.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.connect.SampleDeviceAdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="android.app.device_admin"
android:resource="#xml/device_admin" />
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
<action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
</intent-filter>
</receiver>
<activity
android:name="com.connect.Droidian"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You cant declare two activities as LAUNCHER. change your droidian activity as DEFAULT.
<activity
android:name="com.connect.Droidian"
android:excludeFromRecents="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Add this to your onCreate
Intent intent = new Intent(getApplicationContext(), Droidian.class);
startActivity(intent);
A few issues:
At first, there is the launcher thing Yogesh mentioned. The launcher category in the intent filter means, that this Activity is the one that gets called first, thus there can only exist one launcher.
It isn't exactly clear if you really want Droidian to be a Activity, since you plainly call it a class. Is it meant to have its own view, or should it be a worker class? If the later, don't extend Activity, just make it a regular class. You then can call the functions by instanciating it in your MainActivity like Droidian mDroidian = new Droidian() or making your functions static.
If you really intent to use a Activity and 'call onCreate', you have to start the Activity. Certain methods can't be started manually but are called automatically once you start an Activity, see the Android reference on Activities, especially the 'Activity Lifecycle' section.
The easiest way to start an Activity would be
Intent intent = new Intent(MainActivity.this, Droidian.class);
startActivity(intent);
Also you might want to have a look at the Android Developer trainings, especially the one about starting an activity.
I am testing the following app on HTC with Android 2.3.5. For some reason app is not launching on restarting or booting the phone.
I need to know where am I wrong exactly?
BootReciever.java
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.Toast;
public class BootupReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "App started", Toast.LENGTH_LONG).show();
/*
Intent startActivityIntent = new Intent(context, MainActivity.class);
startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startActivityIntent);
*/
// context.startActivity(new Intent(context, MainActivity.class));
}
}
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.content.BroadcastReceiver;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Android Manifest File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidbootreciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.hello_android_world.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name="com.example.androidbootreciever.BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
The code is working on restart but not on turning on the phone after shutting it down. Here are the messages that I receive in my logcat:
You extended BootupReceiver with Activity?
It should be extended with Broadcast Receiver.
public class BootupReceiver extends BroadcastReceiver{
}
Your Receiver:
public class AutoStartBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent receivedIntent) {
context.startActivity(new Intent(context, YourActivity.class));
}
}
Your Manifest:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver android:name="com.example.receiver.AutoStartBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Good luck!
You need to write following permission,
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
If your application is stored on External storage then you should provide following intent filter,
<receiver android:name=".BootupReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
See App Install Location.
I am using the Device Policy Manager to lock the android phone immediately and here is my activity code:
package com.husam.admin;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
public class AdminActivity extends Activity {
/** Called when the activity is first created. */
protected static final int REQUEST_CODE_ENABLE_ADMIN=1;
DevicePolicyManager dpm;
ComponentName mAdminName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dpm=(DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mAdminName=new ComponentName(this,MyAdmin.class);
Button button=(Button)findViewById(R.id.admin);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(dpm.isAdminActive(mAdminName))
{
Log.w("Yes admin","Locking Now");
dpm.lockNow();
}
else
{Intent intent1 =new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent1.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent1.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Need new Admin");
Log.w("no Admin","Set admin");
startActivityForResult(intent1,REQUEST_CODE_ENABLE_ADMIN);
}
}
});
}
class MyAdmin extends DeviceAdminReceiver{
void OnEnabled(){
}
void onDisable(){
}
}
}
and My Mainfest.xml fill is as follow:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.husam.admin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".AdminActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyAdmin"
android:label="#string/device_admin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/my_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
</application>
</manifest>
and my_admin.xml which is inside xml folder under res folder is as follows:
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
<uses-policies>
<force-lock />
</uses-policies>
</device-admin>
and when I run my app I faced an error in my Log file state that:
W/DeviceAdminAdd(433): Unable to retrieve device policy ComponentInfo{com.husam.admin/com.husam.admin.AdminActivity$MyAdmin}
I searched this website for similar issued but all what I found is that the error was in the mainfest file in closing the receiver before including the meat and intent filter inside it which is not as my case.
So any help or redirection to solve this issue will be completely appreciated
regards
Husam
I think I figured out the answer to the question I posted above. What I need to do is that in registering my receiver in my mainfest file I have to do the following:
<receiver
android:name=".AdminActivity$MyAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="#xml/my_admin"/>
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
</intent-filter>
</receiver>
Note:
in android:name I have to write .AdminActivity$Myadmin instead of .Myadmin, since my class for administration broadcast receiver is an inner class in my activity, not a separate one.
I am new to android, i am trying a broadcast demo, I gave my best effort by reading the documentation but its not working. Please have a look at my code:
BroadcastDemoActivity.java
package com.broadcastdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class BroadcastDemoActivity extends Activity {
/** Called when the activity is first created. */
public static final String PUBLIC_HOLIDAYS = "com.paad.action.PUBLIC_HOLIDAYS";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(getIntent());
}
}
Receive.java
package com.broadcastdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Receive extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String message = intent.getStringExtra("Holiday");
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.broadcastdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".BroadcastDemoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".Receive">
<intent-filter>
<action android:name="com.paad.action.PUBLIC_HOLIDAYS"/>
</intent-filter>
</receiver>
</application>
</manifest>
I know that i am missing something which i am not aware of, please help.
I believe your problem is in the call to sendBroadcast.
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(getIntent());
You are not sending the intent that you construct, you're sending the intent returned from getIntent(), which will be the intent that the activity was started with.
It should be
Intent intent = new Intent(PUBLIC_HOLIDAYS);
intent.putExtra("Holiday", "8th April is a holiday");
sendBroadcast(intent);