kullandığınız android studio sürümü ve gradle versiyonuna göre gradle ve manifest dosyasını güncellemenşz gerekebilir. Aşağıdaki kodlar, Android 12 sisteminde ve gradle 7+ sürümü ile çalışacak şekilde hazırlandı.
manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emine5">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<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/Theme.AppCompat.Light.DarkActionBar">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
mainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telefon Durum Takibi"
android:textSize="24sp"/>
</LinearLayout>
mainActivity.java
package com.example.emine5;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
BroadcastReceiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// ŞARJ DURUMU
if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
Toast.makeText(context,
"Şarj Takıldı ????",
Toast.LENGTH_SHORT).show();
}
if (Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
Toast.makeText(context,
"Şarj Çıkarıldı ❌",
Toast.LENGTH_SHORT).show();
}
// KULAKLIK
if (Intent.ACTION_HEADSET_PLUG.equals(action)) {
int state = intent.getIntExtra("state", -1);
if (state == 1) {
Toast.makeText(context,
"Kulaklık Takıldı ????",
Toast.LENGTH_SHORT).show();
}
if (state == 0) {
Toast.makeText(context,
"Kulaklık Çıkarıldı",
Toast.LENGTH_SHORT).show();
}
}
// İNTERNET
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean connected =
activeNetwork != null &&
activeNetwork.isConnected();
if (connected) {
Toast.makeText(context,
"İnternet Bağlı ????",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,
"İnternet Yok ❌",
Toast.LENGTH_SHORT).show();
}
}
// UÇAK MODU
if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
boolean isOn = intent.getBooleanExtra("state", false);
if (isOn) {
Toast.makeText(context,
"Uçak Modu Açıldı ✈️",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context,
"Uçak Modu Kapatıldı",
Toast.LENGTH_SHORT).show();
}
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(Intent.ACTION_HEADSET_PLUG);
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
registerReceiver(receiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
}
build.gradle (module)
apply plugin: 'com.android.application'
android {
compileSdkVersion 32
defaultConfig {
applicationId "com.example.emine5"
minSdkVersion 16
targetSdkVersion 32
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}