如何制作在后台运行的Android应用程序?

时间:2015-11-11 15:13:30

标签: android android-service

我有一个以下的Android应用程序,用户打印复制的文本...

MainActivity.java

package com.example.backgroundrunning;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Intent;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

@SuppressLint("NewApi") 
public class MainActivity extends Activity implements ClipboardManager.OnPrimaryClipChangedListener{

    ClipboardManager clipBoard;

    TextView tv;
    EditText edit;

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

         tv=(TextView)findViewById(R.id.note);
         edit=(EditText)findViewById(R.id.edit);

        clipBoard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
        clipBoard.addPrimaryClipChangedListener( this );

        startService(new Intent(this,ExampleService.class));

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onPrimaryClipChanged() {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
        String test=clipboard.getText().toString();

        tv.setText(test);

    }
}

ExampleService.java

package com.example.backgroundrunning;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ExampleService extends Service {

    @Override
    public void onCreate() {
        // The service is being created
    }
    public int onStartCommand(Intent intent, int flags, int startId) {
  //      handleCommand(intent);
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }
    @Override
    public void onRebind(Intent intent) {
        // A client is binding to the service with bindService(),
        // after onUnbind() has already been called
    }
    @Override
    public void onDestroy() {
        // The service is no longer used and is being destroyed
    }
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

我想让我的程序在后台默默运行, 意味着当用户按下后退按钮时它不会关闭 我找到了this代码并将其添加到我的程序中,就像上面的代码一样, 但它不起作用......

有什么问题?

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.backgroundrunning"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.backgroundrunning.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

1 个答案:

答案 0 :(得分:1)

@Override
public void onBackPressed()
 { 

    Intent i= new Intent(Intent.ACTION_MAIN);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addCategory(Intent.CATEGORY_HOME);
    startActivity(i); 

  or

    Intent i= new Intent(Intent.ACTION_ALL_APPS);
    startActivity(i); 
}