to day i show you. how to run app in background procces
lets do it
in the firs u can start a new project and create activity.java and layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MenuAwal"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="simpanexternal"android:onClick="strservice"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="bacaexternal"android:onClick="stopservice"/></LinearLayout>
now see in activity.java
public class Activity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout);}public void strservice(){ Intent intent=new Intent(this,ServiceBackground.class); startService(intent); } public void stopservice(){ Intent intent=new Intent(this,ServiceBackground.class); stopService(intent); }}
now create a new activity. java but change the name with ServiceBackground.java
public class ServiceBackground extends Service {
@Override public void onCreate() { super.onCreate(); }@Nullable @Override public IBinder onBind(Intent intent) { return null;}@Override public int onStartCommand(Intent intent, int flags, int startId) {Toast.makeText(this, "start service", Toast.LENGTH_SHORT).show(); return START_STICKY;}@Nullable @Override public IBinder onBind(Intent intent) { return null;}@Overridepublic void onDestroy() { Toast.makeText(this, "Service Destroy", Toast.LENGTH_SHORT).show();} }
ok its finishing add configuration service in manifest file
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blogspot.programer27android.downloadmode">
<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=".Activity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".ServiceBackground" android:exported="false"> </service> </application> </manifest>
now you can try to run your projet..
thanks ..
enjoy your code
Tidak ada komentar:
Posting Komentar