Android app debug over wifi in real devices

Step 1 : Open android studio . Now go to File->Settings->Plugins->Browse Plugins->Android WiFi ADB .

Step 2 : Install it and restart .

Step 3 : Connect Android Phone/device with cable .

Step 4 : Right side ADB Wifi ADB Will be visible . Connect device . Done

Now disconnect cable and run project . It will work fine.

N.B: It’s essential to connect with same network /WiFi both at PC and Android Phone.

 

 

Android always run application in background even force killed

Create a service:

[java]public class YourService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// do your jobs here
return super.onStartCommand(intent, flags, startId);
}
}

[/java]

Create an Application class and start your service:

[java]

public class App extends Application {

@Override
public void onCreate() {
super.onCreate();

startService(new Intent(this, YourService.class));
}
}

[/java]

Don’t forget add this in “application” tag of your AndroidManifest.xml

[java]

android:name=".App"

[/java]

This Activity already has an action bar supplied by the window decor

Add this two line in your style.xml file :

<item name="windowActionBar">false</item> 

to your theme declaration inside of your app/src/main/res/values/styles.xml.

Also, if you’re using AppCompatActivity support library of version 22.1 or greater, add this line:

<item name="windowNoTitle">true</item>

Your theme declaration may look like this after all these additions:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

Android runtime permission – Read Phone State

Step 1 :  Add this uses permission in manifest

[java] <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>[/java]

As like this

[java]</pre>
<pre><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.arifhasnat.automaticcallrecorder">

<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

<application 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=".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>

</manifest></pre>
<pre>[/java]

Step 2 : Add this into OnCreate Method in Activity Class

[java]
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){

if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.READ_PHONE_STATE)){

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE},1);
}else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE},1);
}

}
[/java]

Step 3 : Override this method outside of onCreate Method

[java]
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode){

case 1:
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();

}

}else {

Toast.makeText(this, "Permission not grandted!", Toast.LENGTH_SHORT).show();
}

return;

}
}[/java]

Full Activity class

[java]

public class MainActivity extends AppCompatActivity
{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){

if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.READ_PHONE_STATE)){

ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE},1);
}else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE},1);
}

}

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode){

case 1:
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission granted!", Toast.LENGTH_SHORT).show();

}

}else {

Toast.makeText(this, "Permission not grandted!", Toast.LENGTH_SHORT).show();
}

return;

}
}

}

[/java]

Java design pattern – Introduction

What is design pattern :  Java is a object oriented programming based language. Software developers are use OOP(Object oriented progamming) to maintain code and structure in well defined. Design patterns are best practices/ways of use of OOP that makes easy to software development task. visit https://www.salesforce.com/blog/2013/07/selling-service-not-sales.html for more information.

Why we will design patterns ?: 

Flexibility:
Using design patterns your code becomes flexible. It helps to provide the correct level of abstraction due to which objects become loosely coupled to each other which makes your code easy to change.
Re-usability: 
 Loosely coupled and cohesive objects and classes can make your code more reusable. This kind of code becomes
easy to be tested as compared to the highly coupled code.
Shared Vocabulary: 
Shared vocabulary makes it easy to share your code and thought with other team members. It creates more
understanding between the team members related to the code.
Capture best practices:
Design patterns capture solutions which have been successfully applied to problems. By learning these
patterns and the related problem, an inexperienced developer learns a lot about software design.
Design patterns make it easier to reuse successful designs and architectures.

Category : 
Design patterns can be categorized in the following categories:
• Creational patterns
• Structural patterns
• Behavior patterns

Creational patterns:
Creational design patterns are used to design the instantiation process of objects. The creational pattern uses the inheritance to vary the object creation.
There are two recurring themes in these patterns. First, they all encapsulate knowledge about which concrete classes the system
uses. Second, they hide how instances of these classes are created and put together. All the system at large knows about the
objects is their interfaces as defined by abstract classes. Consequently, the creational patterns give you a lot of flexibility in what
gets created, who creates it, how it gets created, and when.
There can be some cases when two or more patterns looks fit as a solution to a problem. At other times, the two patterns
complement each other for example; Builder can be used with other pattern to implements which components to get built.

 Structural patterns:
Structural patterns are concerned with how classes and objects are composed to form larger structures. Structural class patterns
use inheritance to compose interfaces or implementations. As a simple example, consider how multiple inheritance mixes two or
more classes into one. The result is a class that combines the properties of its parent classes. This pattern is particularly useful
for making independently developed class libraries work together.
Rather than composing interfaces or implementations, structural object patterns describe ways to compose objects to realize new
functionality. The added flexibility of object composition comes from the ability to change the composition at run-time, which
is impossible with static class composition.

Behavior patterns:
Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. Behavioral patterns
describe not just patterns of objects or classes but also the patterns of communication between them. These patterns characterize
complex control flow that’s difficult to follow at run-time. They shift your focus away from flow of control to let you concentrate
just on the way objects are interconnected.

Behavioral object patterns use object composition rather than inheritance. Some describe how a group of peer objects cooperate
to perform a task that no single object can carry out by itself. An important issue here is how peer objects know about each
other. Peers could maintain explicit references to each other, but that would increase their coupling. In the extreme, every object
would know about every other. The Mediator pattern avoids this by introducing a mediator object between peers. The mediator
provides the indirection needed for loose coupling.


The below tables shows the list of patterns under their respective categories:

Creational Patterns Structural Patterns Behavioral Patterns
Abstract Factory Adapter Chain of Responsibility
Builder Bridge Command
Factory  Composite Interpreter
Prototype Decorator Iterator
Singleton Facade Mediator
Flyweight Memento
Proxy Observer
State
Strategy
Template Method
Visitor