'app without icon and with foreground serivce
I created a program without an icon that runs the service by pressing the foreground button and the music is played, but our program is not hidden. It is possible to help
source code frome
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent servIntent = new Intent(this, service.class);
Button start =(Button) findViewById(R.id.start);
Button stop =(Button) findViewById(R.id.stop);
Button hide =(Button) findViewById(R.id.hide);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//for start foreground service
ContextCompat.startForegroundService(MainActivity.this, servIntent);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//for stop service
Intent intent =new Intent(MainActivity.this,service.class);
stopService(intent);
}
});
hide.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//for hide app
PackageManager packageManager=getPackageManager();
ComponentName componentName=new ComponentName(MainActivity.this,MainActivity.class);
packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP);
Toast.makeText(getApplicationContext(), "app hide ", Toast.LENGTH_SHORT).show();
}
});
}
}
Part packageManager is about hiding the program icon
source code form
service.java
public class service extends Service {
private MediaPlayer soundPlayer;
private String CHANNEL_ID = "channelId";
private NotificationManager notifManager;
@Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "servie destroy...!", Toast.LENGTH_SHORT).show();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "سرویس استارت شد", Toast.LENGTH_SHORT).show();
soundPlayer = MediaPlayer.create(this, R.raw.song);
soundPlayer.setLooping(false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String offerChannelName = "Service Channel";
String offerChannelDescription= "Music Channel";
int offerChannelImportance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notifChannel = new NotificationChannel(CHANNEL_ID, offerChannelName, offerChannelImportance);
notifChannel.setDescription(offerChannelDescription);
notifManager = getSystemService(NotificationManager.class);
notifManager.createNotificationChannel(notifChannel);
}
NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.music)
.setContentTitle("relaxing")
.setContentText("playing music ");
Notification servNotification = sNotifBuilder.build();
startForeground(1, servNotification);
soundPlayer.start();
return START_STICKY;
}
}
source code frome
auto_start.java
public class auto_start extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent service = new Intent(context, service.class);
context.startService(service);
}
}}
please help me
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
