-5

The purpose of the app is to show a view on home screen while power connect or disconnect irrespective of app's running status. It should show view on home screen even if the app is not running. PROBLEM: 1: No pop up screen is showing on while app is close.

Note: It shows a popup screen while app is running.

Device : Lollipop

Mode : Developer Mode

Here I have attached the code. The main Activity class is as below. It opens on start of application.

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_CODE_SOME_FEATURES_PERMISSIONS = 101;
static WindowManager.LayoutParams params;
static WindowManager wm;
static ViewGroup lockScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Bundle bundle = getIntent().getExtras();
    try {
        String from = bundle.getString("from");
        if (from.equalsIgnoreCase("broadcast")) {
            startActivity(new Intent(getApplicationContext(), BatteryAdActivity.class));
            finish();

        }
    } catch (Exception ex) {

    }

    getPermission();


}

private void getPermission() {
    String[] PERMISSIONS = {Manifest.permission.BATTERY_STATS, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.REORDER_TASKS};
    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
    }
}

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

}

2:The BroadCast receiver class is as below.

public class PowerConnectionReceiver extends BroadcastReceiver {
Context context;

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    PackageManager pm = context.getPackageManager();
    Intent launchIntent = pm.getLaunchIntentForPackage("com.dogpo.batteryoptimize");
    launchIntent.putExtra("from", "broadcast");
    context.startActivity(launchIntent);

    showMessage("charging");

}

private void showMessage(String charging) {
    Toast.makeText(context, charging, Toast.LENGTH_SHORT).show();
}

}

3: The activity which opens on power disconnect or connect is as below.

public class BatteryAdActivity extends AppCompatActivity {

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

}

4: The Manifest code is as below.

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<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">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".PowerConnectionReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        </intent-filter>
    </receiver>

    <activity android:name=".BatteryAdActivity"
        ></activity>
</application>

10zin
  • 79
  • 4
  • You need to be more specific: which ad network for example – Ishaan Kumar May 06 '17 at 12:59
  • @IshaanKumar The purpose of the app it to show battery information with a google ads in lock screen like the image i attached in my original question. I want to know how to achieve this in android platform. ( I need steps involved in it. especially the popup screen on lockscreen). you can find my source code in https://github.com/tenzinzero1/Testing . – 10zin May 06 '17 at 13:17
  • Do it in xml. whats the problem? – Ishaan Kumar May 06 '17 at 13:42

1 Answers1

-1

Declare a blank Relative Layout of definite size where you want to show the ad. Now:

RelativeLayout rl= (RelativeLayout) 
findViewById(R.id.ad_container); /*ad_container is the id of the RelativeLayout defined in XML*/
AdView mAdView = new AdView(this);
mAdView.setAdUnitId(/*Banner Id*/);
mAdView.setAdSize(AdSize.BANNER);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.addView(mAdView,params);
Ishaan Kumar
  • 968
  • 1
  • 11
  • 24